ConstantFold.cpp 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263
  1. //===- ConstantFold.cpp - LLVM constant folder ----------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements folding of constants for LLVM. This implements the
  11. // (internal) ConstantFold.h interface, which is used by the
  12. // ConstantExpr::get* methods to automatically fold constants when possible.
  13. //
  14. // The current constant folding implementation is implemented in two pieces: the
  15. // pieces that don't need DataLayout, and the pieces that do. This is to avoid
  16. // a dependence in IR on Target.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #include "ConstantFold.h"
  20. #include "llvm/ADT/SmallVector.h"
  21. #include "llvm/IR/Constants.h"
  22. #include "llvm/IR/DerivedTypes.h"
  23. #include "llvm/IR/Function.h"
  24. #include "llvm/IR/GetElementPtrTypeIterator.h"
  25. #include "llvm/IR/GlobalAlias.h"
  26. #include "llvm/IR/GlobalVariable.h"
  27. #include "llvm/IR/Instructions.h"
  28. #include "llvm/IR/Operator.h"
  29. #include "llvm/IR/PatternMatch.h"
  30. #include "llvm/Support/Compiler.h"
  31. #include "llvm/Support/ErrorHandling.h"
  32. #include "llvm/Support/ManagedStatic.h"
  33. #include "llvm/Support/MathExtras.h"
  34. #include <limits>
  35. using namespace llvm;
  36. using namespace llvm::PatternMatch;
  37. //===----------------------------------------------------------------------===//
  38. // ConstantFold*Instruction Implementations
  39. //===----------------------------------------------------------------------===//
  40. /// BitCastConstantVector - Convert the specified vector Constant node to the
  41. /// specified vector type. At this point, we know that the elements of the
  42. /// input vector constant are all simple integer or FP values.
  43. static Constant *BitCastConstantVector(Constant *CV, VectorType *DstTy) {
  44. if (CV->isAllOnesValue()) return Constant::getAllOnesValue(DstTy);
  45. if (CV->isNullValue()) return Constant::getNullValue(DstTy);
  46. // If this cast changes element count then we can't handle it here:
  47. // doing so requires endianness information. This should be handled by
  48. // Analysis/ConstantFolding.cpp
  49. unsigned NumElts = DstTy->getNumElements();
  50. if (NumElts != CV->getType()->getVectorNumElements())
  51. return nullptr;
  52. Type *DstEltTy = DstTy->getElementType();
  53. SmallVector<Constant*, 16> Result;
  54. Type *Ty = IntegerType::get(CV->getContext(), 32);
  55. for (unsigned i = 0; i != NumElts; ++i) {
  56. Constant *C =
  57. ConstantExpr::getExtractElement(CV, ConstantInt::get(Ty, i));
  58. C = ConstantExpr::getBitCast(C, DstEltTy);
  59. Result.push_back(C);
  60. }
  61. return ConstantVector::get(Result);
  62. }
  63. /// This function determines which opcode to use to fold two constant cast
  64. /// expressions together. It uses CastInst::isEliminableCastPair to determine
  65. /// the opcode. Consequently its just a wrapper around that function.
  66. /// @brief Determine if it is valid to fold a cast of a cast
  67. static unsigned
  68. foldConstantCastPair(
  69. unsigned opc, ///< opcode of the second cast constant expression
  70. ConstantExpr *Op, ///< the first cast constant expression
  71. Type *DstTy ///< destination type of the first cast
  72. ) {
  73. assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!");
  74. assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type");
  75. assert(CastInst::isCast(opc) && "Invalid cast opcode");
  76. // The the types and opcodes for the two Cast constant expressions
  77. Type *SrcTy = Op->getOperand(0)->getType();
  78. Type *MidTy = Op->getType();
  79. Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode());
  80. Instruction::CastOps secondOp = Instruction::CastOps(opc);
  81. // Assume that pointers are never more than 64 bits wide, and only use this
  82. // for the middle type. Otherwise we could end up folding away illegal
  83. // bitcasts between address spaces with different sizes.
  84. IntegerType *FakeIntPtrTy = Type::getInt64Ty(DstTy->getContext());
  85. // Let CastInst::isEliminableCastPair do the heavy lifting.
  86. return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy,
  87. nullptr, FakeIntPtrTy, nullptr);
  88. }
  89. static Constant *FoldBitCast(Constant *V, Type *DestTy) {
  90. Type *SrcTy = V->getType();
  91. if (SrcTy == DestTy)
  92. return V; // no-op cast
  93. // Check to see if we are casting a pointer to an aggregate to a pointer to
  94. // the first element. If so, return the appropriate GEP instruction.
  95. if (PointerType *PTy = dyn_cast<PointerType>(V->getType()))
  96. if (PointerType *DPTy = dyn_cast<PointerType>(DestTy))
  97. if (PTy->getAddressSpace() == DPTy->getAddressSpace()
  98. && DPTy->getElementType()->isSized()) {
  99. SmallVector<Value*, 8> IdxList;
  100. Value *Zero =
  101. Constant::getNullValue(Type::getInt32Ty(DPTy->getContext()));
  102. IdxList.push_back(Zero);
  103. Type *ElTy = PTy->getElementType();
  104. while (ElTy != DPTy->getElementType()) {
  105. if (StructType *STy = dyn_cast<StructType>(ElTy)) {
  106. if (STy->getNumElements() == 0) break;
  107. ElTy = STy->getElementType(0);
  108. IdxList.push_back(Zero);
  109. } else if (SequentialType *STy =
  110. dyn_cast<SequentialType>(ElTy)) {
  111. if (ElTy->isPointerTy()) break; // Can't index into pointers!
  112. ElTy = STy->getElementType();
  113. IdxList.push_back(Zero);
  114. } else {
  115. break;
  116. }
  117. }
  118. if (ElTy == DPTy->getElementType())
  119. // This GEP is inbounds because all indices are zero.
  120. return ConstantExpr::getInBoundsGetElementPtr(PTy->getElementType(),
  121. V, IdxList);
  122. }
  123. // Handle casts from one vector constant to another. We know that the src
  124. // and dest type have the same size (otherwise its an illegal cast).
  125. if (VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
  126. if (VectorType *SrcTy = dyn_cast<VectorType>(V->getType())) {
  127. assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() &&
  128. "Not cast between same sized vectors!");
  129. SrcTy = nullptr;
  130. // First, check for null. Undef is already handled.
  131. if (isa<ConstantAggregateZero>(V))
  132. return Constant::getNullValue(DestTy);
  133. // Handle ConstantVector and ConstantAggregateVector.
  134. return BitCastConstantVector(V, DestPTy);
  135. }
  136. // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
  137. // This allows for other simplifications (although some of them
  138. // can only be handled by Analysis/ConstantFolding.cpp).
  139. if (isa<ConstantInt>(V) || isa<ConstantFP>(V))
  140. return ConstantExpr::getBitCast(ConstantVector::get(V), DestPTy);
  141. }
  142. // Finally, implement bitcast folding now. The code below doesn't handle
  143. // bitcast right.
  144. if (isa<ConstantPointerNull>(V)) // ptr->ptr cast.
  145. return ConstantPointerNull::get(cast<PointerType>(DestTy));
  146. // Handle integral constant input.
  147. if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
  148. if (DestTy->isIntegerTy())
  149. // Integral -> Integral. This is a no-op because the bit widths must
  150. // be the same. Consequently, we just fold to V.
  151. return V;
  152. // See note below regarding the PPC_FP128 restriction.
  153. if (DestTy->isFloatingPointTy() && !DestTy->isPPC_FP128Ty())
  154. return ConstantFP::get(DestTy->getContext(),
  155. APFloat(DestTy->getFltSemantics(),
  156. CI->getValue()));
  157. // Otherwise, can't fold this (vector?)
  158. return nullptr;
  159. }
  160. // Handle ConstantFP input: FP -> Integral.
  161. if (ConstantFP *FP = dyn_cast<ConstantFP>(V)) {
  162. // PPC_FP128 is really the sum of two consecutive doubles, where the first
  163. // double is always stored first in memory, regardless of the target
  164. // endianness. The memory layout of i128, however, depends on the target
  165. // endianness, and so we can't fold this without target endianness
  166. // information. This should instead be handled by
  167. // Analysis/ConstantFolding.cpp
  168. if (FP->getType()->isPPC_FP128Ty())
  169. return nullptr;
  170. return ConstantInt::get(FP->getContext(),
  171. FP->getValueAPF().bitcastToAPInt());
  172. }
  173. return nullptr;
  174. }
  175. /// ExtractConstantBytes - V is an integer constant which only has a subset of
  176. /// its bytes used. The bytes used are indicated by ByteStart (which is the
  177. /// first byte used, counting from the least significant byte) and ByteSize,
  178. /// which is the number of bytes used.
  179. ///
  180. /// This function analyzes the specified constant to see if the specified byte
  181. /// range can be returned as a simplified constant. If so, the constant is
  182. /// returned, otherwise null is returned.
  183. ///
  184. static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart,
  185. unsigned ByteSize) {
  186. assert(C->getType()->isIntegerTy() &&
  187. (cast<IntegerType>(C->getType())->getBitWidth() & 7) == 0 &&
  188. "Non-byte sized integer input");
  189. unsigned CSize = cast<IntegerType>(C->getType())->getBitWidth()/8;
  190. assert(ByteSize && "Must be accessing some piece");
  191. assert(ByteStart+ByteSize <= CSize && "Extracting invalid piece from input");
  192. assert(ByteSize != CSize && "Should not extract everything");
  193. // Constant Integers are simple.
  194. if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
  195. APInt V = CI->getValue();
  196. if (ByteStart)
  197. V = V.lshr(ByteStart*8);
  198. V = V.trunc(ByteSize*8);
  199. return ConstantInt::get(CI->getContext(), V);
  200. }
  201. // In the input is a constant expr, we might be able to recursively simplify.
  202. // If not, we definitely can't do anything.
  203. ConstantExpr *CE = dyn_cast<ConstantExpr>(C);
  204. if (!CE) return nullptr;
  205. switch (CE->getOpcode()) {
  206. default: return nullptr;
  207. case Instruction::Or: {
  208. Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize);
  209. if (!RHS)
  210. return nullptr;
  211. // X | -1 -> -1.
  212. if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS))
  213. if (RHSC->isAllOnesValue())
  214. return RHSC;
  215. Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize);
  216. if (!LHS)
  217. return nullptr;
  218. return ConstantExpr::getOr(LHS, RHS);
  219. }
  220. case Instruction::And: {
  221. Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize);
  222. if (!RHS)
  223. return nullptr;
  224. // X & 0 -> 0.
  225. if (RHS->isNullValue())
  226. return RHS;
  227. Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize);
  228. if (!LHS)
  229. return nullptr;
  230. return ConstantExpr::getAnd(LHS, RHS);
  231. }
  232. case Instruction::LShr: {
  233. ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1));
  234. if (!Amt)
  235. return nullptr;
  236. unsigned ShAmt = Amt->getZExtValue();
  237. // Cannot analyze non-byte shifts.
  238. if ((ShAmt & 7) != 0)
  239. return nullptr;
  240. ShAmt >>= 3;
  241. // If the extract is known to be all zeros, return zero.
  242. if (ByteStart >= CSize-ShAmt)
  243. return Constant::getNullValue(IntegerType::get(CE->getContext(),
  244. ByteSize*8));
  245. // If the extract is known to be fully in the input, extract it.
  246. if (ByteStart+ByteSize+ShAmt <= CSize)
  247. return ExtractConstantBytes(CE->getOperand(0), ByteStart+ShAmt, ByteSize);
  248. // TODO: Handle the 'partially zero' case.
  249. return nullptr;
  250. }
  251. case Instruction::Shl: {
  252. ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1));
  253. if (!Amt)
  254. return nullptr;
  255. unsigned ShAmt = Amt->getZExtValue();
  256. // Cannot analyze non-byte shifts.
  257. if ((ShAmt & 7) != 0)
  258. return nullptr;
  259. ShAmt >>= 3;
  260. // If the extract is known to be all zeros, return zero.
  261. if (ByteStart+ByteSize <= ShAmt)
  262. return Constant::getNullValue(IntegerType::get(CE->getContext(),
  263. ByteSize*8));
  264. // If the extract is known to be fully in the input, extract it.
  265. if (ByteStart >= ShAmt)
  266. return ExtractConstantBytes(CE->getOperand(0), ByteStart-ShAmt, ByteSize);
  267. // TODO: Handle the 'partially zero' case.
  268. return nullptr;
  269. }
  270. case Instruction::ZExt: {
  271. unsigned SrcBitSize =
  272. cast<IntegerType>(CE->getOperand(0)->getType())->getBitWidth();
  273. // If extracting something that is completely zero, return 0.
  274. if (ByteStart*8 >= SrcBitSize)
  275. return Constant::getNullValue(IntegerType::get(CE->getContext(),
  276. ByteSize*8));
  277. // If exactly extracting the input, return it.
  278. if (ByteStart == 0 && ByteSize*8 == SrcBitSize)
  279. return CE->getOperand(0);
  280. // If extracting something completely in the input, if if the input is a
  281. // multiple of 8 bits, recurse.
  282. if ((SrcBitSize&7) == 0 && (ByteStart+ByteSize)*8 <= SrcBitSize)
  283. return ExtractConstantBytes(CE->getOperand(0), ByteStart, ByteSize);
  284. // Otherwise, if extracting a subset of the input, which is not multiple of
  285. // 8 bits, do a shift and trunc to get the bits.
  286. if ((ByteStart+ByteSize)*8 < SrcBitSize) {
  287. assert((SrcBitSize&7) && "Shouldn't get byte sized case here");
  288. Constant *Res = CE->getOperand(0);
  289. if (ByteStart)
  290. Res = ConstantExpr::getLShr(Res,
  291. ConstantInt::get(Res->getType(), ByteStart*8));
  292. return ConstantExpr::getTrunc(Res, IntegerType::get(C->getContext(),
  293. ByteSize*8));
  294. }
  295. // TODO: Handle the 'partially zero' case.
  296. return nullptr;
  297. }
  298. }
  299. }
  300. /// getFoldedSizeOf - Return a ConstantExpr with type DestTy for sizeof
  301. /// on Ty, with any known factors factored out. If Folded is false,
  302. /// return null if no factoring was possible, to avoid endlessly
  303. /// bouncing an unfoldable expression back into the top-level folder.
  304. ///
  305. static Constant *getFoldedSizeOf(Type *Ty, Type *DestTy,
  306. bool Folded) {
  307. if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
  308. Constant *N = ConstantInt::get(DestTy, ATy->getNumElements());
  309. Constant *E = getFoldedSizeOf(ATy->getElementType(), DestTy, true);
  310. return ConstantExpr::getNUWMul(E, N);
  311. }
  312. if (StructType *STy = dyn_cast<StructType>(Ty))
  313. if (!STy->isPacked()) {
  314. unsigned NumElems = STy->getNumElements();
  315. // An empty struct has size zero.
  316. if (NumElems == 0)
  317. return ConstantExpr::getNullValue(DestTy);
  318. // Check for a struct with all members having the same size.
  319. Constant *MemberSize =
  320. getFoldedSizeOf(STy->getElementType(0), DestTy, true);
  321. bool AllSame = true;
  322. for (unsigned i = 1; i != NumElems; ++i)
  323. if (MemberSize !=
  324. getFoldedSizeOf(STy->getElementType(i), DestTy, true)) {
  325. AllSame = false;
  326. break;
  327. }
  328. if (AllSame) {
  329. Constant *N = ConstantInt::get(DestTy, NumElems);
  330. return ConstantExpr::getNUWMul(MemberSize, N);
  331. }
  332. }
  333. // Pointer size doesn't depend on the pointee type, so canonicalize them
  334. // to an arbitrary pointee.
  335. if (PointerType *PTy = dyn_cast<PointerType>(Ty))
  336. if (!PTy->getElementType()->isIntegerTy(1))
  337. return
  338. getFoldedSizeOf(PointerType::get(IntegerType::get(PTy->getContext(), 1),
  339. PTy->getAddressSpace()),
  340. DestTy, true);
  341. // If there's no interesting folding happening, bail so that we don't create
  342. // a constant that looks like it needs folding but really doesn't.
  343. if (!Folded)
  344. return nullptr;
  345. // Base case: Get a regular sizeof expression.
  346. Constant *C = ConstantExpr::getSizeOf(Ty);
  347. C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
  348. DestTy, false),
  349. C, DestTy);
  350. return C;
  351. }
  352. /// getFoldedAlignOf - Return a ConstantExpr with type DestTy for alignof
  353. /// on Ty, with any known factors factored out. If Folded is false,
  354. /// return null if no factoring was possible, to avoid endlessly
  355. /// bouncing an unfoldable expression back into the top-level folder.
  356. ///
  357. static Constant *getFoldedAlignOf(Type *Ty, Type *DestTy,
  358. bool Folded) {
  359. // The alignment of an array is equal to the alignment of the
  360. // array element. Note that this is not always true for vectors.
  361. if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
  362. Constant *C = ConstantExpr::getAlignOf(ATy->getElementType());
  363. C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
  364. DestTy,
  365. false),
  366. C, DestTy);
  367. return C;
  368. }
  369. if (StructType *STy = dyn_cast<StructType>(Ty)) {
  370. // Packed structs always have an alignment of 1.
  371. if (STy->isPacked())
  372. return ConstantInt::get(DestTy, 1);
  373. // Otherwise, struct alignment is the maximum alignment of any member.
  374. // Without target data, we can't compare much, but we can check to see
  375. // if all the members have the same alignment.
  376. unsigned NumElems = STy->getNumElements();
  377. // An empty struct has minimal alignment.
  378. if (NumElems == 0)
  379. return ConstantInt::get(DestTy, 1);
  380. // Check for a struct with all members having the same alignment.
  381. Constant *MemberAlign =
  382. getFoldedAlignOf(STy->getElementType(0), DestTy, true);
  383. bool AllSame = true;
  384. for (unsigned i = 1; i != NumElems; ++i)
  385. if (MemberAlign != getFoldedAlignOf(STy->getElementType(i), DestTy, true)) {
  386. AllSame = false;
  387. break;
  388. }
  389. if (AllSame)
  390. return MemberAlign;
  391. }
  392. // Pointer alignment doesn't depend on the pointee type, so canonicalize them
  393. // to an arbitrary pointee.
  394. if (PointerType *PTy = dyn_cast<PointerType>(Ty))
  395. if (!PTy->getElementType()->isIntegerTy(1))
  396. return
  397. getFoldedAlignOf(PointerType::get(IntegerType::get(PTy->getContext(),
  398. 1),
  399. PTy->getAddressSpace()),
  400. DestTy, true);
  401. // If there's no interesting folding happening, bail so that we don't create
  402. // a constant that looks like it needs folding but really doesn't.
  403. if (!Folded)
  404. return nullptr;
  405. // Base case: Get a regular alignof expression.
  406. Constant *C = ConstantExpr::getAlignOf(Ty);
  407. C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
  408. DestTy, false),
  409. C, DestTy);
  410. return C;
  411. }
  412. /// getFoldedOffsetOf - Return a ConstantExpr with type DestTy for offsetof
  413. /// on Ty and FieldNo, with any known factors factored out. If Folded is false,
  414. /// return null if no factoring was possible, to avoid endlessly
  415. /// bouncing an unfoldable expression back into the top-level folder.
  416. ///
  417. static Constant *getFoldedOffsetOf(Type *Ty, Constant *FieldNo,
  418. Type *DestTy,
  419. bool Folded) {
  420. if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
  421. Constant *N = ConstantExpr::getCast(CastInst::getCastOpcode(FieldNo, false,
  422. DestTy, false),
  423. FieldNo, DestTy);
  424. Constant *E = getFoldedSizeOf(ATy->getElementType(), DestTy, true);
  425. return ConstantExpr::getNUWMul(E, N);
  426. }
  427. if (StructType *STy = dyn_cast<StructType>(Ty))
  428. if (!STy->isPacked()) {
  429. unsigned NumElems = STy->getNumElements();
  430. // An empty struct has no members.
  431. if (NumElems == 0)
  432. return nullptr;
  433. // Check for a struct with all members having the same size.
  434. Constant *MemberSize =
  435. getFoldedSizeOf(STy->getElementType(0), DestTy, true);
  436. bool AllSame = true;
  437. for (unsigned i = 1; i != NumElems; ++i)
  438. if (MemberSize !=
  439. getFoldedSizeOf(STy->getElementType(i), DestTy, true)) {
  440. AllSame = false;
  441. break;
  442. }
  443. if (AllSame) {
  444. Constant *N = ConstantExpr::getCast(CastInst::getCastOpcode(FieldNo,
  445. false,
  446. DestTy,
  447. false),
  448. FieldNo, DestTy);
  449. return ConstantExpr::getNUWMul(MemberSize, N);
  450. }
  451. }
  452. // If there's no interesting folding happening, bail so that we don't create
  453. // a constant that looks like it needs folding but really doesn't.
  454. if (!Folded)
  455. return nullptr;
  456. // Base case: Get a regular offsetof expression.
  457. Constant *C = ConstantExpr::getOffsetOf(Ty, FieldNo);
  458. C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
  459. DestTy, false),
  460. C, DestTy);
  461. return C;
  462. }
  463. Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
  464. Type *DestTy) {
  465. if (isa<UndefValue>(V)) {
  466. // zext(undef) = 0, because the top bits will be zero.
  467. // sext(undef) = 0, because the top bits will all be the same.
  468. // [us]itofp(undef) = 0, because the result value is bounded.
  469. if (opc == Instruction::ZExt || opc == Instruction::SExt ||
  470. opc == Instruction::UIToFP || opc == Instruction::SIToFP)
  471. return Constant::getNullValue(DestTy);
  472. return UndefValue::get(DestTy);
  473. }
  474. if (V->isNullValue() && !DestTy->isX86_MMXTy())
  475. return Constant::getNullValue(DestTy);
  476. // If the cast operand is a constant expression, there's a few things we can
  477. // do to try to simplify it.
  478. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
  479. if (CE->isCast()) {
  480. // Try hard to fold cast of cast because they are often eliminable.
  481. if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
  482. return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy);
  483. } else if (CE->getOpcode() == Instruction::GetElementPtr &&
  484. // Do not fold addrspacecast (gep 0, .., 0). It might make the
  485. // addrspacecast uncanonicalized.
  486. opc != Instruction::AddrSpaceCast) {
  487. // If all of the indexes in the GEP are null values, there is no pointer
  488. // adjustment going on. We might as well cast the source pointer.
  489. bool isAllNull = true;
  490. for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
  491. if (!CE->getOperand(i)->isNullValue()) {
  492. isAllNull = false;
  493. break;
  494. }
  495. if (isAllNull)
  496. // This is casting one pointer type to another, always BitCast
  497. return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy);
  498. }
  499. }
  500. // If the cast operand is a constant vector, perform the cast by
  501. // operating on each element. In the cast of bitcasts, the element
  502. // count may be mismatched; don't attempt to handle that here.
  503. if ((isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) &&
  504. DestTy->isVectorTy() &&
  505. DestTy->getVectorNumElements() == V->getType()->getVectorNumElements()) {
  506. SmallVector<Constant*, 16> res;
  507. VectorType *DestVecTy = cast<VectorType>(DestTy);
  508. Type *DstEltTy = DestVecTy->getElementType();
  509. Type *Ty = IntegerType::get(V->getContext(), 32);
  510. for (unsigned i = 0, e = V->getType()->getVectorNumElements(); i != e; ++i) {
  511. Constant *C =
  512. ConstantExpr::getExtractElement(V, ConstantInt::get(Ty, i));
  513. res.push_back(ConstantExpr::getCast(opc, C, DstEltTy));
  514. }
  515. return ConstantVector::get(res);
  516. }
  517. // We actually have to do a cast now. Perform the cast according to the
  518. // opcode specified.
  519. switch (opc) {
  520. default:
  521. llvm_unreachable("Failed to cast constant expression");
  522. case Instruction::FPTrunc:
  523. case Instruction::FPExt:
  524. if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
  525. bool ignored;
  526. APFloat Val = FPC->getValueAPF();
  527. Val.convert(DestTy->isHalfTy() ? APFloat::IEEEhalf :
  528. DestTy->isFloatTy() ? APFloat::IEEEsingle :
  529. DestTy->isDoubleTy() ? APFloat::IEEEdouble :
  530. DestTy->isX86_FP80Ty() ? APFloat::x87DoubleExtended :
  531. DestTy->isFP128Ty() ? APFloat::IEEEquad :
  532. DestTy->isPPC_FP128Ty() ? APFloat::PPCDoubleDouble :
  533. APFloat::Bogus,
  534. APFloat::rmNearestTiesToEven, &ignored);
  535. return ConstantFP::get(V->getContext(), Val);
  536. }
  537. return nullptr; // Can't fold.
  538. case Instruction::FPToUI:
  539. case Instruction::FPToSI:
  540. if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
  541. const APFloat &V = FPC->getValueAPF();
  542. bool ignored;
  543. uint64_t x[2];
  544. uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
  545. if (APFloat::opInvalidOp ==
  546. V.convertToInteger(x, DestBitWidth, opc==Instruction::FPToSI,
  547. APFloat::rmTowardZero, &ignored)) {
  548. // Undefined behavior invoked - the destination type can't represent
  549. // the input constant.
  550. return UndefValue::get(DestTy);
  551. }
  552. APInt Val(DestBitWidth, x);
  553. return ConstantInt::get(FPC->getContext(), Val);
  554. }
  555. return nullptr; // Can't fold.
  556. case Instruction::IntToPtr: //always treated as unsigned
  557. if (V->isNullValue()) // Is it an integral null value?
  558. return ConstantPointerNull::get(cast<PointerType>(DestTy));
  559. return nullptr; // Other pointer types cannot be casted
  560. case Instruction::PtrToInt: // always treated as unsigned
  561. // Is it a null pointer value?
  562. if (V->isNullValue())
  563. return ConstantInt::get(DestTy, 0);
  564. // If this is a sizeof-like expression, pull out multiplications by
  565. // known factors to expose them to subsequent folding. If it's an
  566. // alignof-like expression, factor out known factors.
  567. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
  568. if (CE->getOpcode() == Instruction::GetElementPtr &&
  569. CE->getOperand(0)->isNullValue()) {
  570. GEPOperator *GEPO = cast<GEPOperator>(CE);
  571. Type *Ty = GEPO->getSourceElementType();
  572. if (CE->getNumOperands() == 2) {
  573. // Handle a sizeof-like expression.
  574. Constant *Idx = CE->getOperand(1);
  575. bool isOne = isa<ConstantInt>(Idx) && cast<ConstantInt>(Idx)->isOne();
  576. if (Constant *C = getFoldedSizeOf(Ty, DestTy, !isOne)) {
  577. Idx = ConstantExpr::getCast(CastInst::getCastOpcode(Idx, true,
  578. DestTy, false),
  579. Idx, DestTy);
  580. return ConstantExpr::getMul(C, Idx);
  581. }
  582. } else if (CE->getNumOperands() == 3 &&
  583. CE->getOperand(1)->isNullValue()) {
  584. // Handle an alignof-like expression.
  585. if (StructType *STy = dyn_cast<StructType>(Ty))
  586. if (!STy->isPacked()) {
  587. ConstantInt *CI = cast<ConstantInt>(CE->getOperand(2));
  588. if (CI->isOne() &&
  589. STy->getNumElements() == 2 &&
  590. STy->getElementType(0)->isIntegerTy(1)) {
  591. return getFoldedAlignOf(STy->getElementType(1), DestTy, false);
  592. }
  593. }
  594. // Handle an offsetof-like expression.
  595. if (Ty->isStructTy() || Ty->isArrayTy()) {
  596. if (Constant *C = getFoldedOffsetOf(Ty, CE->getOperand(2),
  597. DestTy, false))
  598. return C;
  599. }
  600. }
  601. }
  602. // Other pointer types cannot be casted
  603. return nullptr;
  604. case Instruction::UIToFP:
  605. case Instruction::SIToFP:
  606. if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
  607. APInt api = CI->getValue();
  608. APFloat apf(DestTy->getFltSemantics(),
  609. APInt::getNullValue(DestTy->getPrimitiveSizeInBits()));
  610. if (APFloat::opOverflow &
  611. apf.convertFromAPInt(api, opc==Instruction::SIToFP,
  612. APFloat::rmNearestTiesToEven)) {
  613. // Undefined behavior invoked - the destination type can't represent
  614. // the input constant.
  615. return UndefValue::get(DestTy);
  616. }
  617. return ConstantFP::get(V->getContext(), apf);
  618. }
  619. return nullptr;
  620. case Instruction::ZExt:
  621. if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
  622. uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
  623. return ConstantInt::get(V->getContext(),
  624. CI->getValue().zext(BitWidth));
  625. }
  626. return nullptr;
  627. case Instruction::SExt:
  628. if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
  629. uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
  630. return ConstantInt::get(V->getContext(),
  631. CI->getValue().sext(BitWidth));
  632. }
  633. return nullptr;
  634. case Instruction::Trunc: {
  635. if (V->getType()->isVectorTy())
  636. return nullptr;
  637. uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
  638. if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
  639. return ConstantInt::get(V->getContext(),
  640. CI->getValue().trunc(DestBitWidth));
  641. }
  642. // The input must be a constantexpr. See if we can simplify this based on
  643. // the bytes we are demanding. Only do this if the source and dest are an
  644. // even multiple of a byte.
  645. if ((DestBitWidth & 7) == 0 &&
  646. (cast<IntegerType>(V->getType())->getBitWidth() & 7) == 0)
  647. if (Constant *Res = ExtractConstantBytes(V, 0, DestBitWidth / 8))
  648. return Res;
  649. return nullptr;
  650. }
  651. case Instruction::BitCast:
  652. return FoldBitCast(V, DestTy);
  653. case Instruction::AddrSpaceCast:
  654. return nullptr;
  655. }
  656. }
  657. Constant *llvm::ConstantFoldSelectInstruction(Constant *Cond,
  658. Constant *V1, Constant *V2) {
  659. // Check for i1 and vector true/false conditions.
  660. if (Cond->isNullValue()) return V2;
  661. if (Cond->isAllOnesValue()) return V1;
  662. // If the condition is a vector constant, fold the result elementwise.
  663. if (ConstantVector *CondV = dyn_cast<ConstantVector>(Cond)) {
  664. SmallVector<Constant*, 16> Result;
  665. Type *Ty = IntegerType::get(CondV->getContext(), 32);
  666. for (unsigned i = 0, e = V1->getType()->getVectorNumElements(); i != e;++i){
  667. Constant *V;
  668. Constant *V1Element = ConstantExpr::getExtractElement(V1,
  669. ConstantInt::get(Ty, i));
  670. Constant *V2Element = ConstantExpr::getExtractElement(V2,
  671. ConstantInt::get(Ty, i));
  672. Constant *Cond = dyn_cast<Constant>(CondV->getOperand(i));
  673. if (V1Element == V2Element) {
  674. V = V1Element;
  675. } else if (isa<UndefValue>(Cond)) {
  676. V = isa<UndefValue>(V1Element) ? V1Element : V2Element;
  677. } else {
  678. if (!isa<ConstantInt>(Cond)) break;
  679. V = Cond->isNullValue() ? V2Element : V1Element;
  680. }
  681. Result.push_back(V);
  682. }
  683. // If we were able to build the vector, return it.
  684. if (Result.size() == V1->getType()->getVectorNumElements())
  685. return ConstantVector::get(Result);
  686. }
  687. if (isa<UndefValue>(Cond)) {
  688. if (isa<UndefValue>(V1)) return V1;
  689. return V2;
  690. }
  691. if (isa<UndefValue>(V1)) return V2;
  692. if (isa<UndefValue>(V2)) return V1;
  693. if (V1 == V2) return V1;
  694. if (ConstantExpr *TrueVal = dyn_cast<ConstantExpr>(V1)) {
  695. if (TrueVal->getOpcode() == Instruction::Select)
  696. if (TrueVal->getOperand(0) == Cond)
  697. return ConstantExpr::getSelect(Cond, TrueVal->getOperand(1), V2);
  698. }
  699. if (ConstantExpr *FalseVal = dyn_cast<ConstantExpr>(V2)) {
  700. if (FalseVal->getOpcode() == Instruction::Select)
  701. if (FalseVal->getOperand(0) == Cond)
  702. return ConstantExpr::getSelect(Cond, V1, FalseVal->getOperand(2));
  703. }
  704. return nullptr;
  705. }
  706. Constant *llvm::ConstantFoldExtractElementInstruction(Constant *Val,
  707. Constant *Idx) {
  708. if (isa<UndefValue>(Val)) // ee(undef, x) -> undef
  709. return UndefValue::get(Val->getType()->getVectorElementType());
  710. if (Val->isNullValue()) // ee(zero, x) -> zero
  711. return Constant::getNullValue(Val->getType()->getVectorElementType());
  712. // ee({w,x,y,z}, undef) -> undef
  713. if (isa<UndefValue>(Idx))
  714. return UndefValue::get(Val->getType()->getVectorElementType());
  715. if (ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
  716. // ee({w,x,y,z}, wrong_value) -> undef
  717. if (CIdx->uge(Val->getType()->getVectorNumElements()))
  718. return UndefValue::get(Val->getType()->getVectorElementType());
  719. return Val->getAggregateElement(CIdx->getZExtValue());
  720. }
  721. return nullptr;
  722. }
  723. Constant *llvm::ConstantFoldInsertElementInstruction(Constant *Val,
  724. Constant *Elt,
  725. Constant *Idx) {
  726. if (isa<UndefValue>(Idx))
  727. return UndefValue::get(Val->getType());
  728. ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
  729. if (!CIdx) return nullptr;
  730. unsigned NumElts = Val->getType()->getVectorNumElements();
  731. if (CIdx->uge(NumElts))
  732. return UndefValue::get(Val->getType());
  733. SmallVector<Constant*, 16> Result;
  734. Result.reserve(NumElts);
  735. auto *Ty = Type::getInt32Ty(Val->getContext());
  736. uint64_t IdxVal = CIdx->getZExtValue();
  737. for (unsigned i = 0; i != NumElts; ++i) {
  738. if (i == IdxVal) {
  739. Result.push_back(Elt);
  740. continue;
  741. }
  742. Constant *C = ConstantExpr::getExtractElement(Val, ConstantInt::get(Ty, i));
  743. Result.push_back(C);
  744. }
  745. return ConstantVector::get(Result);
  746. }
  747. Constant *llvm::ConstantFoldShuffleVectorInstruction(Constant *V1,
  748. Constant *V2,
  749. Constant *Mask) {
  750. unsigned MaskNumElts = Mask->getType()->getVectorNumElements();
  751. Type *EltTy = V1->getType()->getVectorElementType();
  752. // Undefined shuffle mask -> undefined value.
  753. if (isa<UndefValue>(Mask))
  754. return UndefValue::get(VectorType::get(EltTy, MaskNumElts));
  755. // Don't break the bitcode reader hack.
  756. if (isa<ConstantExpr>(Mask)) return nullptr;
  757. unsigned SrcNumElts = V1->getType()->getVectorNumElements();
  758. // Loop over the shuffle mask, evaluating each element.
  759. SmallVector<Constant*, 32> Result;
  760. for (unsigned i = 0; i != MaskNumElts; ++i) {
  761. int Elt = ShuffleVectorInst::getMaskValue(Mask, i);
  762. if (Elt == -1) {
  763. Result.push_back(UndefValue::get(EltTy));
  764. continue;
  765. }
  766. Constant *InElt;
  767. if (unsigned(Elt) >= SrcNumElts*2)
  768. InElt = UndefValue::get(EltTy);
  769. else if (unsigned(Elt) >= SrcNumElts) {
  770. Type *Ty = IntegerType::get(V2->getContext(), 32);
  771. InElt =
  772. ConstantExpr::getExtractElement(V2,
  773. ConstantInt::get(Ty, Elt - SrcNumElts));
  774. } else {
  775. Type *Ty = IntegerType::get(V1->getContext(), 32);
  776. InElt = ConstantExpr::getExtractElement(V1, ConstantInt::get(Ty, Elt));
  777. }
  778. Result.push_back(InElt);
  779. }
  780. return ConstantVector::get(Result);
  781. }
  782. Constant *llvm::ConstantFoldExtractValueInstruction(Constant *Agg,
  783. ArrayRef<unsigned> Idxs) {
  784. // Base case: no indices, so return the entire value.
  785. if (Idxs.empty())
  786. return Agg;
  787. if (Constant *C = Agg->getAggregateElement(Idxs[0]))
  788. return ConstantFoldExtractValueInstruction(C, Idxs.slice(1));
  789. return nullptr;
  790. }
  791. Constant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg,
  792. Constant *Val,
  793. ArrayRef<unsigned> Idxs) {
  794. // Base case: no indices, so replace the entire value.
  795. if (Idxs.empty())
  796. return Val;
  797. unsigned NumElts;
  798. if (StructType *ST = dyn_cast<StructType>(Agg->getType()))
  799. NumElts = ST->getNumElements();
  800. else if (ArrayType *AT = dyn_cast<ArrayType>(Agg->getType()))
  801. NumElts = AT->getNumElements();
  802. else
  803. NumElts = Agg->getType()->getVectorNumElements();
  804. SmallVector<Constant*, 32> Result;
  805. for (unsigned i = 0; i != NumElts; ++i) {
  806. Constant *C = Agg->getAggregateElement(i);
  807. if (!C) return nullptr;
  808. if (Idxs[0] == i)
  809. C = ConstantFoldInsertValueInstruction(C, Val, Idxs.slice(1));
  810. Result.push_back(C);
  811. }
  812. if (StructType *ST = dyn_cast<StructType>(Agg->getType()))
  813. return ConstantStruct::get(ST, Result);
  814. if (ArrayType *AT = dyn_cast<ArrayType>(Agg->getType()))
  815. return ConstantArray::get(AT, Result);
  816. return ConstantVector::get(Result);
  817. }
  818. Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
  819. Constant *C1, Constant *C2) {
  820. // Handle UndefValue up front.
  821. if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
  822. switch (Opcode) {
  823. case Instruction::Xor:
  824. if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
  825. // Handle undef ^ undef -> 0 special case. This is a common
  826. // idiom (misuse).
  827. return Constant::getNullValue(C1->getType());
  828. // Fallthrough
  829. case Instruction::Add:
  830. case Instruction::Sub:
  831. return UndefValue::get(C1->getType());
  832. case Instruction::And:
  833. if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef & undef -> undef
  834. return C1;
  835. return Constant::getNullValue(C1->getType()); // undef & X -> 0
  836. case Instruction::Mul: {
  837. // undef * undef -> undef
  838. if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
  839. return C1;
  840. const APInt *CV;
  841. // X * undef -> undef if X is odd
  842. if (match(C1, m_APInt(CV)) || match(C2, m_APInt(CV)))
  843. if ((*CV)[0])
  844. return UndefValue::get(C1->getType());
  845. // X * undef -> 0 otherwise
  846. return Constant::getNullValue(C1->getType());
  847. }
  848. case Instruction::SDiv:
  849. case Instruction::UDiv:
  850. // X / undef -> undef
  851. if (match(C1, m_Zero()))
  852. return C2;
  853. // undef / 0 -> undef
  854. // undef / 1 -> undef
  855. if (match(C2, m_Zero()) || match(C2, m_One()))
  856. return C1;
  857. // undef / X -> 0 otherwise
  858. return Constant::getNullValue(C1->getType());
  859. case Instruction::URem:
  860. case Instruction::SRem:
  861. // X % undef -> undef
  862. if (match(C2, m_Undef()))
  863. return C2;
  864. // undef % 0 -> undef
  865. if (match(C2, m_Zero()))
  866. return C1;
  867. // undef % X -> 0 otherwise
  868. return Constant::getNullValue(C1->getType());
  869. case Instruction::Or: // X | undef -> -1
  870. if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef | undef -> undef
  871. return C1;
  872. return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0
  873. case Instruction::LShr:
  874. // X >>l undef -> undef
  875. if (isa<UndefValue>(C2))
  876. return C2;
  877. // undef >>l 0 -> undef
  878. if (match(C2, m_Zero()))
  879. return C1;
  880. // undef >>l X -> 0
  881. return Constant::getNullValue(C1->getType());
  882. case Instruction::AShr:
  883. // X >>a undef -> undef
  884. if (isa<UndefValue>(C2))
  885. return C2;
  886. // undef >>a 0 -> undef
  887. if (match(C2, m_Zero()))
  888. return C1;
  889. // TODO: undef >>a X -> undef if the shift is exact
  890. // undef >>a X -> 0
  891. return Constant::getNullValue(C1->getType());
  892. case Instruction::Shl:
  893. // X << undef -> undef
  894. if (isa<UndefValue>(C2))
  895. return C2;
  896. // undef << 0 -> undef
  897. if (match(C2, m_Zero()))
  898. return C1;
  899. // undef << X -> 0
  900. return Constant::getNullValue(C1->getType());
  901. }
  902. }
  903. // Handle simplifications when the RHS is a constant int.
  904. if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
  905. switch (Opcode) {
  906. case Instruction::Add:
  907. if (CI2->equalsInt(0)) return C1; // X + 0 == X
  908. break;
  909. case Instruction::Sub:
  910. if (CI2->equalsInt(0)) return C1; // X - 0 == X
  911. break;
  912. case Instruction::Mul:
  913. if (CI2->equalsInt(0)) return C2; // X * 0 == 0
  914. if (CI2->equalsInt(1))
  915. return C1; // X * 1 == X
  916. break;
  917. case Instruction::UDiv:
  918. case Instruction::SDiv:
  919. if (CI2->equalsInt(1))
  920. return C1; // X / 1 == X
  921. if (CI2->equalsInt(0))
  922. return UndefValue::get(CI2->getType()); // X / 0 == undef
  923. break;
  924. case Instruction::URem:
  925. case Instruction::SRem:
  926. if (CI2->equalsInt(1))
  927. return Constant::getNullValue(CI2->getType()); // X % 1 == 0
  928. if (CI2->equalsInt(0))
  929. return UndefValue::get(CI2->getType()); // X % 0 == undef
  930. break;
  931. case Instruction::And:
  932. if (CI2->isZero()) return C2; // X & 0 == 0
  933. if (CI2->isAllOnesValue())
  934. return C1; // X & -1 == X
  935. if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
  936. // (zext i32 to i64) & 4294967295 -> (zext i32 to i64)
  937. if (CE1->getOpcode() == Instruction::ZExt) {
  938. unsigned DstWidth = CI2->getType()->getBitWidth();
  939. unsigned SrcWidth =
  940. CE1->getOperand(0)->getType()->getPrimitiveSizeInBits();
  941. APInt PossiblySetBits(APInt::getLowBitsSet(DstWidth, SrcWidth));
  942. if ((PossiblySetBits & CI2->getValue()) == PossiblySetBits)
  943. return C1;
  944. }
  945. // If and'ing the address of a global with a constant, fold it.
  946. if (CE1->getOpcode() == Instruction::PtrToInt &&
  947. isa<GlobalValue>(CE1->getOperand(0))) {
  948. GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
  949. // Functions are at least 4-byte aligned.
  950. unsigned GVAlign = GV->getAlignment();
  951. if (isa<Function>(GV))
  952. GVAlign = std::max(GVAlign, 4U);
  953. if (GVAlign > 1) {
  954. unsigned DstWidth = CI2->getType()->getBitWidth();
  955. unsigned SrcWidth = std::min(DstWidth, Log2_32(GVAlign));
  956. APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth));
  957. // If checking bits we know are clear, return zero.
  958. if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
  959. return Constant::getNullValue(CI2->getType());
  960. }
  961. }
  962. }
  963. break;
  964. case Instruction::Or:
  965. if (CI2->equalsInt(0)) return C1; // X | 0 == X
  966. if (CI2->isAllOnesValue())
  967. return C2; // X | -1 == -1
  968. break;
  969. case Instruction::Xor:
  970. if (CI2->equalsInt(0)) return C1; // X ^ 0 == X
  971. if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
  972. switch (CE1->getOpcode()) {
  973. default: break;
  974. case Instruction::ICmp:
  975. case Instruction::FCmp:
  976. // cmp pred ^ true -> cmp !pred
  977. assert(CI2->equalsInt(1));
  978. CmpInst::Predicate pred = (CmpInst::Predicate)CE1->getPredicate();
  979. pred = CmpInst::getInversePredicate(pred);
  980. return ConstantExpr::getCompare(pred, CE1->getOperand(0),
  981. CE1->getOperand(1));
  982. }
  983. }
  984. break;
  985. case Instruction::AShr:
  986. // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2
  987. if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1))
  988. if (CE1->getOpcode() == Instruction::ZExt) // Top bits known zero.
  989. return ConstantExpr::getLShr(C1, C2);
  990. break;
  991. }
  992. } else if (isa<ConstantInt>(C1)) {
  993. // If C1 is a ConstantInt and C2 is not, swap the operands.
  994. if (Instruction::isCommutative(Opcode))
  995. return ConstantExpr::get(Opcode, C2, C1);
  996. }
  997. // At this point we know neither constant is an UndefValue.
  998. if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
  999. if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
  1000. const APInt &C1V = CI1->getValue();
  1001. const APInt &C2V = CI2->getValue();
  1002. switch (Opcode) {
  1003. default:
  1004. break;
  1005. case Instruction::Add:
  1006. return ConstantInt::get(CI1->getContext(), C1V + C2V);
  1007. case Instruction::Sub:
  1008. return ConstantInt::get(CI1->getContext(), C1V - C2V);
  1009. case Instruction::Mul:
  1010. return ConstantInt::get(CI1->getContext(), C1V * C2V);
  1011. case Instruction::UDiv:
  1012. assert(!CI2->isNullValue() && "Div by zero handled above");
  1013. return ConstantInt::get(CI1->getContext(), C1V.udiv(C2V));
  1014. case Instruction::SDiv:
  1015. assert(!CI2->isNullValue() && "Div by zero handled above");
  1016. if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
  1017. return UndefValue::get(CI1->getType()); // MIN_INT / -1 -> undef
  1018. return ConstantInt::get(CI1->getContext(), C1V.sdiv(C2V));
  1019. case Instruction::URem:
  1020. assert(!CI2->isNullValue() && "Div by zero handled above");
  1021. return ConstantInt::get(CI1->getContext(), C1V.urem(C2V));
  1022. case Instruction::SRem:
  1023. assert(!CI2->isNullValue() && "Div by zero handled above");
  1024. if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
  1025. return UndefValue::get(CI1->getType()); // MIN_INT % -1 -> undef
  1026. return ConstantInt::get(CI1->getContext(), C1V.srem(C2V));
  1027. case Instruction::And:
  1028. return ConstantInt::get(CI1->getContext(), C1V & C2V);
  1029. case Instruction::Or:
  1030. return ConstantInt::get(CI1->getContext(), C1V | C2V);
  1031. case Instruction::Xor:
  1032. return ConstantInt::get(CI1->getContext(), C1V ^ C2V);
  1033. case Instruction::Shl:
  1034. if (C2V.ult(C1V.getBitWidth()))
  1035. return ConstantInt::get(CI1->getContext(), C1V.shl(C2V));
  1036. return UndefValue::get(C1->getType()); // too big shift is undef
  1037. case Instruction::LShr:
  1038. if (C2V.ult(C1V.getBitWidth()))
  1039. return ConstantInt::get(CI1->getContext(), C1V.lshr(C2V));
  1040. return UndefValue::get(C1->getType()); // too big shift is undef
  1041. case Instruction::AShr:
  1042. if (C2V.ult(C1V.getBitWidth()))
  1043. return ConstantInt::get(CI1->getContext(), C1V.ashr(C2V));
  1044. return UndefValue::get(C1->getType()); // too big shift is undef
  1045. }
  1046. }
  1047. switch (Opcode) {
  1048. case Instruction::SDiv:
  1049. case Instruction::UDiv:
  1050. case Instruction::URem:
  1051. case Instruction::SRem:
  1052. case Instruction::LShr:
  1053. case Instruction::AShr:
  1054. case Instruction::Shl:
  1055. if (CI1->equalsInt(0)) return C1;
  1056. break;
  1057. default:
  1058. break;
  1059. }
  1060. } else if (ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
  1061. if (ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
  1062. APFloat C1V = CFP1->getValueAPF();
  1063. APFloat C2V = CFP2->getValueAPF();
  1064. APFloat C3V = C1V; // copy for modification
  1065. switch (Opcode) {
  1066. default:
  1067. break;
  1068. case Instruction::FAdd:
  1069. (void)C3V.add(C2V, APFloat::rmNearestTiesToEven);
  1070. return ConstantFP::get(C1->getContext(), C3V);
  1071. case Instruction::FSub:
  1072. (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven);
  1073. return ConstantFP::get(C1->getContext(), C3V);
  1074. case Instruction::FMul:
  1075. (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven);
  1076. return ConstantFP::get(C1->getContext(), C3V);
  1077. case Instruction::FDiv:
  1078. (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
  1079. return ConstantFP::get(C1->getContext(), C3V);
  1080. case Instruction::FRem:
  1081. (void)C3V.mod(C2V, APFloat::rmNearestTiesToEven);
  1082. return ConstantFP::get(C1->getContext(), C3V);
  1083. }
  1084. }
  1085. } else if (VectorType *VTy = dyn_cast<VectorType>(C1->getType())) {
  1086. // Perform elementwise folding.
  1087. SmallVector<Constant*, 16> Result;
  1088. Type *Ty = IntegerType::get(VTy->getContext(), 32);
  1089. for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
  1090. Constant *LHS =
  1091. ConstantExpr::getExtractElement(C1, ConstantInt::get(Ty, i));
  1092. Constant *RHS =
  1093. ConstantExpr::getExtractElement(C2, ConstantInt::get(Ty, i));
  1094. Result.push_back(ConstantExpr::get(Opcode, LHS, RHS));
  1095. }
  1096. return ConstantVector::get(Result);
  1097. }
  1098. if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
  1099. // There are many possible foldings we could do here. We should probably
  1100. // at least fold add of a pointer with an integer into the appropriate
  1101. // getelementptr. This will improve alias analysis a bit.
  1102. // Given ((a + b) + c), if (b + c) folds to something interesting, return
  1103. // (a + (b + c)).
  1104. if (Instruction::isAssociative(Opcode) && CE1->getOpcode() == Opcode) {
  1105. Constant *T = ConstantExpr::get(Opcode, CE1->getOperand(1), C2);
  1106. if (!isa<ConstantExpr>(T) || cast<ConstantExpr>(T)->getOpcode() != Opcode)
  1107. return ConstantExpr::get(Opcode, CE1->getOperand(0), T);
  1108. }
  1109. } else if (isa<ConstantExpr>(C2)) {
  1110. // If C2 is a constant expr and C1 isn't, flop them around and fold the
  1111. // other way if possible.
  1112. if (Instruction::isCommutative(Opcode))
  1113. return ConstantFoldBinaryInstruction(Opcode, C2, C1);
  1114. }
  1115. // i1 can be simplified in many cases.
  1116. if (C1->getType()->isIntegerTy(1)) {
  1117. switch (Opcode) {
  1118. case Instruction::Add:
  1119. case Instruction::Sub:
  1120. return ConstantExpr::getXor(C1, C2);
  1121. case Instruction::Mul:
  1122. return ConstantExpr::getAnd(C1, C2);
  1123. case Instruction::Shl:
  1124. case Instruction::LShr:
  1125. case Instruction::AShr:
  1126. // We can assume that C2 == 0. If it were one the result would be
  1127. // undefined because the shift value is as large as the bitwidth.
  1128. return C1;
  1129. case Instruction::SDiv:
  1130. case Instruction::UDiv:
  1131. // We can assume that C2 == 1. If it were zero the result would be
  1132. // undefined through division by zero.
  1133. return C1;
  1134. case Instruction::URem:
  1135. case Instruction::SRem:
  1136. // We can assume that C2 == 1. If it were zero the result would be
  1137. // undefined through division by zero.
  1138. return ConstantInt::getFalse(C1->getContext());
  1139. default:
  1140. break;
  1141. }
  1142. }
  1143. // We don't know how to fold this.
  1144. return nullptr;
  1145. }
  1146. /// isZeroSizedType - This type is zero sized if its an array or structure of
  1147. /// zero sized types. The only leaf zero sized type is an empty structure.
  1148. static bool isMaybeZeroSizedType(Type *Ty) {
  1149. if (StructType *STy = dyn_cast<StructType>(Ty)) {
  1150. if (STy->isOpaque()) return true; // Can't say.
  1151. // If all of elements have zero size, this does too.
  1152. for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
  1153. if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
  1154. return true;
  1155. } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
  1156. return isMaybeZeroSizedType(ATy->getElementType());
  1157. }
  1158. return false;
  1159. }
  1160. /// IdxCompare - Compare the two constants as though they were getelementptr
  1161. /// indices. This allows coersion of the types to be the same thing.
  1162. ///
  1163. /// If the two constants are the "same" (after coersion), return 0. If the
  1164. /// first is less than the second, return -1, if the second is less than the
  1165. /// first, return 1. If the constants are not integral, return -2.
  1166. ///
  1167. static int IdxCompare(Constant *C1, Constant *C2, Type *ElTy) {
  1168. if (C1 == C2) return 0;
  1169. // Ok, we found a different index. If they are not ConstantInt, we can't do
  1170. // anything with them.
  1171. if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
  1172. return -2; // don't know!
  1173. // We cannot compare the indices if they don't fit in an int64_t.
  1174. if (cast<ConstantInt>(C1)->getValue().getActiveBits() > 64 ||
  1175. cast<ConstantInt>(C2)->getValue().getActiveBits() > 64)
  1176. return -2; // don't know!
  1177. // Ok, we have two differing integer indices. Sign extend them to be the same
  1178. // type.
  1179. int64_t C1Val = cast<ConstantInt>(C1)->getSExtValue();
  1180. int64_t C2Val = cast<ConstantInt>(C2)->getSExtValue();
  1181. if (C1Val == C2Val) return 0; // They are equal
  1182. // If the type being indexed over is really just a zero sized type, there is
  1183. // no pointer difference being made here.
  1184. if (isMaybeZeroSizedType(ElTy))
  1185. return -2; // dunno.
  1186. // If they are really different, now that they are the same type, then we
  1187. // found a difference!
  1188. if (C1Val < C2Val)
  1189. return -1;
  1190. else
  1191. return 1;
  1192. }
  1193. /// evaluateFCmpRelation - This function determines if there is anything we can
  1194. /// decide about the two constants provided. This doesn't need to handle simple
  1195. /// things like ConstantFP comparisons, but should instead handle ConstantExprs.
  1196. /// If we can determine that the two constants have a particular relation to
  1197. /// each other, we should return the corresponding FCmpInst predicate,
  1198. /// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in
  1199. /// ConstantFoldCompareInstruction.
  1200. ///
  1201. /// To simplify this code we canonicalize the relation so that the first
  1202. /// operand is always the most "complex" of the two. We consider ConstantFP
  1203. /// to be the simplest, and ConstantExprs to be the most complex.
  1204. static FCmpInst::Predicate evaluateFCmpRelation(Constant *V1, Constant *V2) {
  1205. assert(V1->getType() == V2->getType() &&
  1206. "Cannot compare values of different types!");
  1207. // Handle degenerate case quickly
  1208. if (V1 == V2) return FCmpInst::FCMP_OEQ;
  1209. if (!isa<ConstantExpr>(V1)) {
  1210. if (!isa<ConstantExpr>(V2)) {
  1211. // Simple case, use the standard constant folder.
  1212. ConstantInt *R = nullptr;
  1213. R = dyn_cast<ConstantInt>(
  1214. ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, V1, V2));
  1215. if (R && !R->isZero())
  1216. return FCmpInst::FCMP_OEQ;
  1217. R = dyn_cast<ConstantInt>(
  1218. ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, V1, V2));
  1219. if (R && !R->isZero())
  1220. return FCmpInst::FCMP_OLT;
  1221. R = dyn_cast<ConstantInt>(
  1222. ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, V1, V2));
  1223. if (R && !R->isZero())
  1224. return FCmpInst::FCMP_OGT;
  1225. // Nothing more we can do
  1226. return FCmpInst::BAD_FCMP_PREDICATE;
  1227. }
  1228. // If the first operand is simple and second is ConstantExpr, swap operands.
  1229. FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(V2, V1);
  1230. if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE)
  1231. return FCmpInst::getSwappedPredicate(SwappedRelation);
  1232. } else {
  1233. // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
  1234. // constantexpr or a simple constant.
  1235. ConstantExpr *CE1 = cast<ConstantExpr>(V1);
  1236. switch (CE1->getOpcode()) {
  1237. case Instruction::FPTrunc:
  1238. case Instruction::FPExt:
  1239. case Instruction::UIToFP:
  1240. case Instruction::SIToFP:
  1241. // We might be able to do something with these but we don't right now.
  1242. break;
  1243. default:
  1244. break;
  1245. }
  1246. }
  1247. // There are MANY other foldings that we could perform here. They will
  1248. // probably be added on demand, as they seem needed.
  1249. return FCmpInst::BAD_FCMP_PREDICATE;
  1250. }
  1251. static ICmpInst::Predicate areGlobalsPotentiallyEqual(const GlobalValue *GV1,
  1252. const GlobalValue *GV2) {
  1253. auto isGlobalUnsafeForEquality = [](const GlobalValue *GV) {
  1254. if (GV->hasExternalWeakLinkage() || GV->hasWeakAnyLinkage())
  1255. return true;
  1256. if (const auto *GVar = dyn_cast<GlobalVariable>(GV)) {
  1257. Type *Ty = GVar->getValueType();
  1258. // A global with opaque type might end up being zero sized.
  1259. if (!Ty->isSized())
  1260. return true;
  1261. // A global with an empty type might lie at the address of any other
  1262. // global.
  1263. if (Ty->isEmptyTy())
  1264. return true;
  1265. }
  1266. return false;
  1267. };
  1268. // Don't try to decide equality of aliases.
  1269. if (!isa<GlobalAlias>(GV1) && !isa<GlobalAlias>(GV2))
  1270. if (!isGlobalUnsafeForEquality(GV1) && !isGlobalUnsafeForEquality(GV2))
  1271. return ICmpInst::ICMP_NE;
  1272. return ICmpInst::BAD_ICMP_PREDICATE;
  1273. }
  1274. /// evaluateICmpRelation - This function determines if there is anything we can
  1275. /// decide about the two constants provided. This doesn't need to handle simple
  1276. /// things like integer comparisons, but should instead handle ConstantExprs
  1277. /// and GlobalValues. If we can determine that the two constants have a
  1278. /// particular relation to each other, we should return the corresponding ICmp
  1279. /// predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE.
  1280. ///
  1281. /// To simplify this code we canonicalize the relation so that the first
  1282. /// operand is always the most "complex" of the two. We consider simple
  1283. /// constants (like ConstantInt) to be the simplest, followed by
  1284. /// GlobalValues, followed by ConstantExpr's (the most complex).
  1285. ///
  1286. static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2,
  1287. bool isSigned) {
  1288. assert(V1->getType() == V2->getType() &&
  1289. "Cannot compare different types of values!");
  1290. if (V1 == V2) return ICmpInst::ICMP_EQ;
  1291. if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1) &&
  1292. !isa<BlockAddress>(V1)) {
  1293. if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2) &&
  1294. !isa<BlockAddress>(V2)) {
  1295. // We distilled this down to a simple case, use the standard constant
  1296. // folder.
  1297. ConstantInt *R = nullptr;
  1298. ICmpInst::Predicate pred = ICmpInst::ICMP_EQ;
  1299. R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
  1300. if (R && !R->isZero())
  1301. return pred;
  1302. pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
  1303. R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
  1304. if (R && !R->isZero())
  1305. return pred;
  1306. pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
  1307. R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
  1308. if (R && !R->isZero())
  1309. return pred;
  1310. // If we couldn't figure it out, bail.
  1311. return ICmpInst::BAD_ICMP_PREDICATE;
  1312. }
  1313. // If the first operand is simple, swap operands.
  1314. ICmpInst::Predicate SwappedRelation =
  1315. evaluateICmpRelation(V2, V1, isSigned);
  1316. if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
  1317. return ICmpInst::getSwappedPredicate(SwappedRelation);
  1318. } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
  1319. if (isa<ConstantExpr>(V2)) { // Swap as necessary.
  1320. ICmpInst::Predicate SwappedRelation =
  1321. evaluateICmpRelation(V2, V1, isSigned);
  1322. if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
  1323. return ICmpInst::getSwappedPredicate(SwappedRelation);
  1324. return ICmpInst::BAD_ICMP_PREDICATE;
  1325. }
  1326. // Now we know that the RHS is a GlobalValue, BlockAddress or simple
  1327. // constant (which, since the types must match, means that it's a
  1328. // ConstantPointerNull).
  1329. if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
  1330. return areGlobalsPotentiallyEqual(GV, GV2);
  1331. } else if (isa<BlockAddress>(V2)) {
  1332. return ICmpInst::ICMP_NE; // Globals never equal labels.
  1333. } else {
  1334. assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
  1335. // GlobalVals can never be null unless they have external weak linkage.
  1336. // We don't try to evaluate aliases here.
  1337. if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV))
  1338. return ICmpInst::ICMP_NE;
  1339. }
  1340. } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
  1341. if (isa<ConstantExpr>(V2)) { // Swap as necessary.
  1342. ICmpInst::Predicate SwappedRelation =
  1343. evaluateICmpRelation(V2, V1, isSigned);
  1344. if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
  1345. return ICmpInst::getSwappedPredicate(SwappedRelation);
  1346. return ICmpInst::BAD_ICMP_PREDICATE;
  1347. }
  1348. // Now we know that the RHS is a GlobalValue, BlockAddress or simple
  1349. // constant (which, since the types must match, means that it is a
  1350. // ConstantPointerNull).
  1351. if (const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
  1352. // Block address in another function can't equal this one, but block
  1353. // addresses in the current function might be the same if blocks are
  1354. // empty.
  1355. if (BA2->getFunction() != BA->getFunction())
  1356. return ICmpInst::ICMP_NE;
  1357. } else {
  1358. // Block addresses aren't null, don't equal the address of globals.
  1359. assert((isa<ConstantPointerNull>(V2) || isa<GlobalValue>(V2)) &&
  1360. "Canonicalization guarantee!");
  1361. return ICmpInst::ICMP_NE;
  1362. }
  1363. } else {
  1364. // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
  1365. // constantexpr, a global, block address, or a simple constant.
  1366. ConstantExpr *CE1 = cast<ConstantExpr>(V1);
  1367. Constant *CE1Op0 = CE1->getOperand(0);
  1368. switch (CE1->getOpcode()) {
  1369. case Instruction::Trunc:
  1370. case Instruction::FPTrunc:
  1371. case Instruction::FPExt:
  1372. case Instruction::FPToUI:
  1373. case Instruction::FPToSI:
  1374. break; // We can't evaluate floating point casts or truncations.
  1375. case Instruction::UIToFP:
  1376. case Instruction::SIToFP:
  1377. case Instruction::BitCast:
  1378. case Instruction::ZExt:
  1379. case Instruction::SExt:
  1380. // If the cast is not actually changing bits, and the second operand is a
  1381. // null pointer, do the comparison with the pre-casted value.
  1382. if (V2->isNullValue() &&
  1383. (CE1->getType()->isPointerTy() || CE1->getType()->isIntegerTy())) {
  1384. if (CE1->getOpcode() == Instruction::ZExt) isSigned = false;
  1385. if (CE1->getOpcode() == Instruction::SExt) isSigned = true;
  1386. return evaluateICmpRelation(CE1Op0,
  1387. Constant::getNullValue(CE1Op0->getType()),
  1388. isSigned);
  1389. }
  1390. break;
  1391. case Instruction::GetElementPtr: {
  1392. GEPOperator *CE1GEP = cast<GEPOperator>(CE1);
  1393. // Ok, since this is a getelementptr, we know that the constant has a
  1394. // pointer type. Check the various cases.
  1395. if (isa<ConstantPointerNull>(V2)) {
  1396. // If we are comparing a GEP to a null pointer, check to see if the base
  1397. // of the GEP equals the null pointer.
  1398. if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
  1399. if (GV->hasExternalWeakLinkage())
  1400. // Weak linkage GVals could be zero or not. We're comparing that
  1401. // to null pointer so its greater-or-equal
  1402. return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
  1403. else
  1404. // If its not weak linkage, the GVal must have a non-zero address
  1405. // so the result is greater-than
  1406. return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
  1407. } else if (isa<ConstantPointerNull>(CE1Op0)) {
  1408. // If we are indexing from a null pointer, check to see if we have any
  1409. // non-zero indices.
  1410. for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
  1411. if (!CE1->getOperand(i)->isNullValue())
  1412. // Offsetting from null, must not be equal.
  1413. return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
  1414. // Only zero indexes from null, must still be zero.
  1415. return ICmpInst::ICMP_EQ;
  1416. }
  1417. // Otherwise, we can't really say if the first operand is null or not.
  1418. } else if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
  1419. if (isa<ConstantPointerNull>(CE1Op0)) {
  1420. if (GV2->hasExternalWeakLinkage())
  1421. // Weak linkage GVals could be zero or not. We're comparing it to
  1422. // a null pointer, so its less-or-equal
  1423. return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
  1424. else
  1425. // If its not weak linkage, the GVal must have a non-zero address
  1426. // so the result is less-than
  1427. return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
  1428. } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
  1429. if (GV == GV2) {
  1430. // If this is a getelementptr of the same global, then it must be
  1431. // different. Because the types must match, the getelementptr could
  1432. // only have at most one index, and because we fold getelementptr's
  1433. // with a single zero index, it must be nonzero.
  1434. assert(CE1->getNumOperands() == 2 &&
  1435. !CE1->getOperand(1)->isNullValue() &&
  1436. "Surprising getelementptr!");
  1437. return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
  1438. } else {
  1439. if (CE1GEP->hasAllZeroIndices())
  1440. return areGlobalsPotentiallyEqual(GV, GV2);
  1441. return ICmpInst::BAD_ICMP_PREDICATE;
  1442. }
  1443. }
  1444. } else {
  1445. ConstantExpr *CE2 = cast<ConstantExpr>(V2);
  1446. Constant *CE2Op0 = CE2->getOperand(0);
  1447. // There are MANY other foldings that we could perform here. They will
  1448. // probably be added on demand, as they seem needed.
  1449. switch (CE2->getOpcode()) {
  1450. default: break;
  1451. case Instruction::GetElementPtr:
  1452. // By far the most common case to handle is when the base pointers are
  1453. // obviously to the same global.
  1454. if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
  1455. // Don't know relative ordering, but check for inequality.
  1456. if (CE1Op0 != CE2Op0) {
  1457. GEPOperator *CE2GEP = cast<GEPOperator>(CE2);
  1458. if (CE1GEP->hasAllZeroIndices() && CE2GEP->hasAllZeroIndices())
  1459. return areGlobalsPotentiallyEqual(cast<GlobalValue>(CE1Op0),
  1460. cast<GlobalValue>(CE2Op0));
  1461. return ICmpInst::BAD_ICMP_PREDICATE;
  1462. }
  1463. // Ok, we know that both getelementptr instructions are based on the
  1464. // same global. From this, we can precisely determine the relative
  1465. // ordering of the resultant pointers.
  1466. unsigned i = 1;
  1467. // The logic below assumes that the result of the comparison
  1468. // can be determined by finding the first index that differs.
  1469. // This doesn't work if there is over-indexing in any
  1470. // subsequent indices, so check for that case first.
  1471. if (!CE1->isGEPWithNoNotionalOverIndexing() ||
  1472. !CE2->isGEPWithNoNotionalOverIndexing())
  1473. return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
  1474. // Compare all of the operands the GEP's have in common.
  1475. gep_type_iterator GTI = gep_type_begin(CE1);
  1476. for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
  1477. ++i, ++GTI)
  1478. switch (IdxCompare(CE1->getOperand(i),
  1479. CE2->getOperand(i), GTI.getIndexedType())) {
  1480. case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT;
  1481. case 1: return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT;
  1482. case -2: return ICmpInst::BAD_ICMP_PREDICATE;
  1483. }
  1484. // Ok, we ran out of things they have in common. If any leftovers
  1485. // are non-zero then we have a difference, otherwise we are equal.
  1486. for (; i < CE1->getNumOperands(); ++i)
  1487. if (!CE1->getOperand(i)->isNullValue()) {
  1488. if (isa<ConstantInt>(CE1->getOperand(i)))
  1489. return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
  1490. else
  1491. return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
  1492. }
  1493. for (; i < CE2->getNumOperands(); ++i)
  1494. if (!CE2->getOperand(i)->isNullValue()) {
  1495. if (isa<ConstantInt>(CE2->getOperand(i)))
  1496. return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
  1497. else
  1498. return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
  1499. }
  1500. return ICmpInst::ICMP_EQ;
  1501. }
  1502. }
  1503. }
  1504. }
  1505. default:
  1506. break;
  1507. }
  1508. }
  1509. return ICmpInst::BAD_ICMP_PREDICATE;
  1510. }
  1511. Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred,
  1512. Constant *C1, Constant *C2) {
  1513. Type *ResultTy;
  1514. if (VectorType *VT = dyn_cast<VectorType>(C1->getType()))
  1515. ResultTy = VectorType::get(Type::getInt1Ty(C1->getContext()),
  1516. VT->getNumElements());
  1517. else
  1518. ResultTy = Type::getInt1Ty(C1->getContext());
  1519. // Fold FCMP_FALSE/FCMP_TRUE unconditionally.
  1520. if (pred == FCmpInst::FCMP_FALSE)
  1521. return Constant::getNullValue(ResultTy);
  1522. if (pred == FCmpInst::FCMP_TRUE)
  1523. return Constant::getAllOnesValue(ResultTy);
  1524. // Handle some degenerate cases first
  1525. if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
  1526. CmpInst::Predicate Predicate = CmpInst::Predicate(pred);
  1527. bool isIntegerPredicate = ICmpInst::isIntPredicate(Predicate);
  1528. // For EQ and NE, we can always pick a value for the undef to make the
  1529. // predicate pass or fail, so we can return undef.
  1530. // Also, if both operands are undef, we can return undef for int comparison.
  1531. if (ICmpInst::isEquality(Predicate) || (isIntegerPredicate && C1 == C2))
  1532. return UndefValue::get(ResultTy);
  1533. // Otherwise, for integer compare, pick the same value as the non-undef
  1534. // operand, and fold it to true or false.
  1535. if (isIntegerPredicate)
  1536. return ConstantInt::get(ResultTy, CmpInst::isTrueWhenEqual(pred));
  1537. // Choosing NaN for the undef will always make unordered comparison succeed
  1538. // and ordered comparison fails.
  1539. return ConstantInt::get(ResultTy, CmpInst::isUnordered(Predicate));
  1540. }
  1541. // icmp eq/ne(null,GV) -> false/true
  1542. if (C1->isNullValue()) {
  1543. if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
  1544. // Don't try to evaluate aliases. External weak GV can be null.
  1545. if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
  1546. if (pred == ICmpInst::ICMP_EQ)
  1547. return ConstantInt::getFalse(C1->getContext());
  1548. else if (pred == ICmpInst::ICMP_NE)
  1549. return ConstantInt::getTrue(C1->getContext());
  1550. }
  1551. // icmp eq/ne(GV,null) -> false/true
  1552. } else if (C2->isNullValue()) {
  1553. if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
  1554. // Don't try to evaluate aliases. External weak GV can be null.
  1555. if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
  1556. if (pred == ICmpInst::ICMP_EQ)
  1557. return ConstantInt::getFalse(C1->getContext());
  1558. else if (pred == ICmpInst::ICMP_NE)
  1559. return ConstantInt::getTrue(C1->getContext());
  1560. }
  1561. }
  1562. // If the comparison is a comparison between two i1's, simplify it.
  1563. if (C1->getType()->isIntegerTy(1)) {
  1564. switch(pred) {
  1565. case ICmpInst::ICMP_EQ:
  1566. if (isa<ConstantInt>(C2))
  1567. return ConstantExpr::getXor(C1, ConstantExpr::getNot(C2));
  1568. return ConstantExpr::getXor(ConstantExpr::getNot(C1), C2);
  1569. case ICmpInst::ICMP_NE:
  1570. return ConstantExpr::getXor(C1, C2);
  1571. default:
  1572. break;
  1573. }
  1574. }
  1575. if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
  1576. APInt V1 = cast<ConstantInt>(C1)->getValue();
  1577. APInt V2 = cast<ConstantInt>(C2)->getValue();
  1578. switch (pred) {
  1579. default: llvm_unreachable("Invalid ICmp Predicate");
  1580. case ICmpInst::ICMP_EQ: return ConstantInt::get(ResultTy, V1 == V2);
  1581. case ICmpInst::ICMP_NE: return ConstantInt::get(ResultTy, V1 != V2);
  1582. case ICmpInst::ICMP_SLT: return ConstantInt::get(ResultTy, V1.slt(V2));
  1583. case ICmpInst::ICMP_SGT: return ConstantInt::get(ResultTy, V1.sgt(V2));
  1584. case ICmpInst::ICMP_SLE: return ConstantInt::get(ResultTy, V1.sle(V2));
  1585. case ICmpInst::ICMP_SGE: return ConstantInt::get(ResultTy, V1.sge(V2));
  1586. case ICmpInst::ICMP_ULT: return ConstantInt::get(ResultTy, V1.ult(V2));
  1587. case ICmpInst::ICMP_UGT: return ConstantInt::get(ResultTy, V1.ugt(V2));
  1588. case ICmpInst::ICMP_ULE: return ConstantInt::get(ResultTy, V1.ule(V2));
  1589. case ICmpInst::ICMP_UGE: return ConstantInt::get(ResultTy, V1.uge(V2));
  1590. }
  1591. } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
  1592. APFloat C1V = cast<ConstantFP>(C1)->getValueAPF();
  1593. APFloat C2V = cast<ConstantFP>(C2)->getValueAPF();
  1594. APFloat::cmpResult R = C1V.compare(C2V);
  1595. switch (pred) {
  1596. default: llvm_unreachable("Invalid FCmp Predicate");
  1597. case FCmpInst::FCMP_FALSE: return Constant::getNullValue(ResultTy);
  1598. case FCmpInst::FCMP_TRUE: return Constant::getAllOnesValue(ResultTy);
  1599. case FCmpInst::FCMP_UNO:
  1600. return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered);
  1601. case FCmpInst::FCMP_ORD:
  1602. return ConstantInt::get(ResultTy, R!=APFloat::cmpUnordered);
  1603. case FCmpInst::FCMP_UEQ:
  1604. return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
  1605. R==APFloat::cmpEqual);
  1606. case FCmpInst::FCMP_OEQ:
  1607. return ConstantInt::get(ResultTy, R==APFloat::cmpEqual);
  1608. case FCmpInst::FCMP_UNE:
  1609. return ConstantInt::get(ResultTy, R!=APFloat::cmpEqual);
  1610. case FCmpInst::FCMP_ONE:
  1611. return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan ||
  1612. R==APFloat::cmpGreaterThan);
  1613. case FCmpInst::FCMP_ULT:
  1614. return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
  1615. R==APFloat::cmpLessThan);
  1616. case FCmpInst::FCMP_OLT:
  1617. return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan);
  1618. case FCmpInst::FCMP_UGT:
  1619. return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
  1620. R==APFloat::cmpGreaterThan);
  1621. case FCmpInst::FCMP_OGT:
  1622. return ConstantInt::get(ResultTy, R==APFloat::cmpGreaterThan);
  1623. case FCmpInst::FCMP_ULE:
  1624. return ConstantInt::get(ResultTy, R!=APFloat::cmpGreaterThan);
  1625. case FCmpInst::FCMP_OLE:
  1626. return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan ||
  1627. R==APFloat::cmpEqual);
  1628. case FCmpInst::FCMP_UGE:
  1629. return ConstantInt::get(ResultTy, R!=APFloat::cmpLessThan);
  1630. case FCmpInst::FCMP_OGE:
  1631. return ConstantInt::get(ResultTy, R==APFloat::cmpGreaterThan ||
  1632. R==APFloat::cmpEqual);
  1633. }
  1634. } else if (C1->getType()->isVectorTy()) {
  1635. // If we can constant fold the comparison of each element, constant fold
  1636. // the whole vector comparison.
  1637. SmallVector<Constant*, 4> ResElts;
  1638. Type *Ty = IntegerType::get(C1->getContext(), 32);
  1639. // Compare the elements, producing an i1 result or constant expr.
  1640. for (unsigned i = 0, e = C1->getType()->getVectorNumElements(); i != e;++i){
  1641. Constant *C1E =
  1642. ConstantExpr::getExtractElement(C1, ConstantInt::get(Ty, i));
  1643. Constant *C2E =
  1644. ConstantExpr::getExtractElement(C2, ConstantInt::get(Ty, i));
  1645. ResElts.push_back(ConstantExpr::getCompare(pred, C1E, C2E));
  1646. }
  1647. return ConstantVector::get(ResElts);
  1648. }
  1649. if (C1->getType()->isFloatingPointTy() &&
  1650. // Only call evaluateFCmpRelation if we have a constant expr to avoid
  1651. // infinite recursive loop
  1652. (isa<ConstantExpr>(C1) || isa<ConstantExpr>(C2))) {
  1653. int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
  1654. switch (evaluateFCmpRelation(C1, C2)) {
  1655. default: llvm_unreachable("Unknown relation!");
  1656. case FCmpInst::FCMP_UNO:
  1657. case FCmpInst::FCMP_ORD:
  1658. case FCmpInst::FCMP_UEQ:
  1659. case FCmpInst::FCMP_UNE:
  1660. case FCmpInst::FCMP_ULT:
  1661. case FCmpInst::FCMP_UGT:
  1662. case FCmpInst::FCMP_ULE:
  1663. case FCmpInst::FCMP_UGE:
  1664. case FCmpInst::FCMP_TRUE:
  1665. case FCmpInst::FCMP_FALSE:
  1666. case FCmpInst::BAD_FCMP_PREDICATE:
  1667. break; // Couldn't determine anything about these constants.
  1668. case FCmpInst::FCMP_OEQ: // We know that C1 == C2
  1669. Result = (pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ ||
  1670. pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE ||
  1671. pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
  1672. break;
  1673. case FCmpInst::FCMP_OLT: // We know that C1 < C2
  1674. Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
  1675. pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT ||
  1676. pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE);
  1677. break;
  1678. case FCmpInst::FCMP_OGT: // We know that C1 > C2
  1679. Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
  1680. pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT ||
  1681. pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
  1682. break;
  1683. case FCmpInst::FCMP_OLE: // We know that C1 <= C2
  1684. // We can only partially decide this relation.
  1685. if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
  1686. Result = 0;
  1687. else if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
  1688. Result = 1;
  1689. break;
  1690. case FCmpInst::FCMP_OGE: // We known that C1 >= C2
  1691. // We can only partially decide this relation.
  1692. if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
  1693. Result = 0;
  1694. else if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
  1695. Result = 1;
  1696. break;
  1697. case FCmpInst::FCMP_ONE: // We know that C1 != C2
  1698. // We can only partially decide this relation.
  1699. if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ)
  1700. Result = 0;
  1701. else if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE)
  1702. Result = 1;
  1703. break;
  1704. }
  1705. // If we evaluated the result, return it now.
  1706. if (Result != -1)
  1707. return ConstantInt::get(ResultTy, Result);
  1708. } else {
  1709. // Evaluate the relation between the two constants, per the predicate.
  1710. int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
  1711. switch (evaluateICmpRelation(C1, C2, CmpInst::isSigned(pred))) {
  1712. default: llvm_unreachable("Unknown relational!");
  1713. case ICmpInst::BAD_ICMP_PREDICATE:
  1714. break; // Couldn't determine anything about these constants.
  1715. case ICmpInst::ICMP_EQ: // We know the constants are equal!
  1716. // If we know the constants are equal, we can decide the result of this
  1717. // computation precisely.
  1718. Result = ICmpInst::isTrueWhenEqual((ICmpInst::Predicate)pred);
  1719. break;
  1720. case ICmpInst::ICMP_ULT:
  1721. switch (pred) {
  1722. case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_ULE:
  1723. Result = 1; break;
  1724. case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_UGE:
  1725. Result = 0; break;
  1726. }
  1727. break;
  1728. case ICmpInst::ICMP_SLT:
  1729. switch (pred) {
  1730. case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SLE:
  1731. Result = 1; break;
  1732. case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SGE:
  1733. Result = 0; break;
  1734. }
  1735. break;
  1736. case ICmpInst::ICMP_UGT:
  1737. switch (pred) {
  1738. case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGE:
  1739. Result = 1; break;
  1740. case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_ULE:
  1741. Result = 0; break;
  1742. }
  1743. break;
  1744. case ICmpInst::ICMP_SGT:
  1745. switch (pred) {
  1746. case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SGE:
  1747. Result = 1; break;
  1748. case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SLE:
  1749. Result = 0; break;
  1750. }
  1751. break;
  1752. case ICmpInst::ICMP_ULE:
  1753. if (pred == ICmpInst::ICMP_UGT) Result = 0;
  1754. if (pred == ICmpInst::ICMP_ULT || pred == ICmpInst::ICMP_ULE) Result = 1;
  1755. break;
  1756. case ICmpInst::ICMP_SLE:
  1757. if (pred == ICmpInst::ICMP_SGT) Result = 0;
  1758. if (pred == ICmpInst::ICMP_SLT || pred == ICmpInst::ICMP_SLE) Result = 1;
  1759. break;
  1760. case ICmpInst::ICMP_UGE:
  1761. if (pred == ICmpInst::ICMP_ULT) Result = 0;
  1762. if (pred == ICmpInst::ICMP_UGT || pred == ICmpInst::ICMP_UGE) Result = 1;
  1763. break;
  1764. case ICmpInst::ICMP_SGE:
  1765. if (pred == ICmpInst::ICMP_SLT) Result = 0;
  1766. if (pred == ICmpInst::ICMP_SGT || pred == ICmpInst::ICMP_SGE) Result = 1;
  1767. break;
  1768. case ICmpInst::ICMP_NE:
  1769. if (pred == ICmpInst::ICMP_EQ) Result = 0;
  1770. if (pred == ICmpInst::ICMP_NE) Result = 1;
  1771. break;
  1772. }
  1773. // If we evaluated the result, return it now.
  1774. if (Result != -1)
  1775. return ConstantInt::get(ResultTy, Result);
  1776. // If the right hand side is a bitcast, try using its inverse to simplify
  1777. // it by moving it to the left hand side. We can't do this if it would turn
  1778. // a vector compare into a scalar compare or visa versa.
  1779. if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(C2)) {
  1780. Constant *CE2Op0 = CE2->getOperand(0);
  1781. if (CE2->getOpcode() == Instruction::BitCast &&
  1782. CE2->getType()->isVectorTy() == CE2Op0->getType()->isVectorTy()) {
  1783. Constant *Inverse = ConstantExpr::getBitCast(C1, CE2Op0->getType());
  1784. return ConstantExpr::getICmp(pred, Inverse, CE2Op0);
  1785. }
  1786. }
  1787. // If the left hand side is an extension, try eliminating it.
  1788. if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
  1789. if ((CE1->getOpcode() == Instruction::SExt && ICmpInst::isSigned(pred)) ||
  1790. (CE1->getOpcode() == Instruction::ZExt && !ICmpInst::isSigned(pred))){
  1791. Constant *CE1Op0 = CE1->getOperand(0);
  1792. Constant *CE1Inverse = ConstantExpr::getTrunc(CE1, CE1Op0->getType());
  1793. if (CE1Inverse == CE1Op0) {
  1794. // Check whether we can safely truncate the right hand side.
  1795. Constant *C2Inverse = ConstantExpr::getTrunc(C2, CE1Op0->getType());
  1796. if (ConstantExpr::getCast(CE1->getOpcode(), C2Inverse,
  1797. C2->getType()) == C2)
  1798. return ConstantExpr::getICmp(pred, CE1Inverse, C2Inverse);
  1799. }
  1800. }
  1801. }
  1802. if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) ||
  1803. (C1->isNullValue() && !C2->isNullValue())) {
  1804. // If C2 is a constant expr and C1 isn't, flip them around and fold the
  1805. // other way if possible.
  1806. // Also, if C1 is null and C2 isn't, flip them around.
  1807. pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred);
  1808. return ConstantExpr::getICmp(pred, C2, C1);
  1809. }
  1810. }
  1811. return nullptr;
  1812. }
  1813. /// isInBoundsIndices - Test whether the given sequence of *normalized* indices
  1814. /// is "inbounds".
  1815. template<typename IndexTy>
  1816. static bool isInBoundsIndices(ArrayRef<IndexTy> Idxs) {
  1817. // No indices means nothing that could be out of bounds.
  1818. if (Idxs.empty()) return true;
  1819. // If the first index is zero, it's in bounds.
  1820. if (cast<Constant>(Idxs[0])->isNullValue()) return true;
  1821. // If the first index is one and all the rest are zero, it's in bounds,
  1822. // by the one-past-the-end rule.
  1823. if (!cast<ConstantInt>(Idxs[0])->isOne())
  1824. return false;
  1825. for (unsigned i = 1, e = Idxs.size(); i != e; ++i)
  1826. if (!cast<Constant>(Idxs[i])->isNullValue())
  1827. return false;
  1828. return true;
  1829. }
  1830. /// \brief Test whether a given ConstantInt is in-range for a SequentialType.
  1831. static bool isIndexInRangeOfSequentialType(const SequentialType *STy,
  1832. const ConstantInt *CI) {
  1833. if (const PointerType *PTy = dyn_cast<PointerType>(STy))
  1834. // Only handle pointers to sized types, not pointers to functions.
  1835. return PTy->getElementType()->isSized();
  1836. uint64_t NumElements = 0;
  1837. // Determine the number of elements in our sequential type.
  1838. if (const ArrayType *ATy = dyn_cast<ArrayType>(STy))
  1839. NumElements = ATy->getNumElements();
  1840. else if (const VectorType *VTy = dyn_cast<VectorType>(STy))
  1841. NumElements = VTy->getNumElements();
  1842. assert((isa<ArrayType>(STy) || NumElements > 0) &&
  1843. "didn't expect non-array type to have zero elements!");
  1844. // We cannot bounds check the index if it doesn't fit in an int64_t.
  1845. if (CI->getValue().getActiveBits() > 64)
  1846. return false;
  1847. // A negative index or an index past the end of our sequential type is
  1848. // considered out-of-range.
  1849. int64_t IndexVal = CI->getSExtValue();
  1850. if (IndexVal < 0 || (NumElements > 0 && (uint64_t)IndexVal >= NumElements))
  1851. return false;
  1852. // Otherwise, it is in-range.
  1853. return true;
  1854. }
  1855. template<typename IndexTy>
  1856. static Constant *ConstantFoldGetElementPtrImpl(Type *PointeeTy, Constant *C,
  1857. bool inBounds,
  1858. ArrayRef<IndexTy> Idxs) {
  1859. if (Idxs.empty()) return C;
  1860. Constant *Idx0 = cast<Constant>(Idxs[0]);
  1861. if ((Idxs.size() == 1 && Idx0->isNullValue()))
  1862. return C;
  1863. if (isa<UndefValue>(C)) {
  1864. PointerType *Ptr = cast<PointerType>(C->getType());
  1865. Type *Ty = GetElementPtrInst::getIndexedType(
  1866. cast<PointerType>(Ptr->getScalarType())->getElementType(), Idxs);
  1867. assert(Ty && "Invalid indices for GEP!");
  1868. return UndefValue::get(PointerType::get(Ty, Ptr->getAddressSpace()));
  1869. }
  1870. if (C->isNullValue()) {
  1871. bool isNull = true;
  1872. for (unsigned i = 0, e = Idxs.size(); i != e; ++i)
  1873. if (!cast<Constant>(Idxs[i])->isNullValue()) {
  1874. isNull = false;
  1875. break;
  1876. }
  1877. if (isNull) {
  1878. PointerType *Ptr = cast<PointerType>(C->getType());
  1879. Type *Ty = GetElementPtrInst::getIndexedType(
  1880. cast<PointerType>(Ptr->getScalarType())->getElementType(), Idxs);
  1881. assert(Ty && "Invalid indices for GEP!");
  1882. return ConstantPointerNull::get(PointerType::get(Ty,
  1883. Ptr->getAddressSpace()));
  1884. }
  1885. }
  1886. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
  1887. // Combine Indices - If the source pointer to this getelementptr instruction
  1888. // is a getelementptr instruction, combine the indices of the two
  1889. // getelementptr instructions into a single instruction.
  1890. //
  1891. if (CE->getOpcode() == Instruction::GetElementPtr) {
  1892. Type *LastTy = nullptr;
  1893. for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
  1894. I != E; ++I)
  1895. LastTy = *I;
  1896. // We cannot combine indices if doing so would take us outside of an
  1897. // array or vector. Doing otherwise could trick us if we evaluated such a
  1898. // GEP as part of a load.
  1899. //
  1900. // e.g. Consider if the original GEP was:
  1901. // i8* getelementptr ({ [2 x i8], i32, i8, [3 x i8] }* @main.c,
  1902. // i32 0, i32 0, i64 0)
  1903. //
  1904. // If we then tried to offset it by '8' to get to the third element,
  1905. // an i8, we should *not* get:
  1906. // i8* getelementptr ({ [2 x i8], i32, i8, [3 x i8] }* @main.c,
  1907. // i32 0, i32 0, i64 8)
  1908. //
  1909. // This GEP tries to index array element '8 which runs out-of-bounds.
  1910. // Subsequent evaluation would get confused and produce erroneous results.
  1911. //
  1912. // The following prohibits such a GEP from being formed by checking to see
  1913. // if the index is in-range with respect to an array or vector.
  1914. bool PerformFold = false;
  1915. if (Idx0->isNullValue())
  1916. PerformFold = true;
  1917. else if (SequentialType *STy = dyn_cast_or_null<SequentialType>(LastTy))
  1918. if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx0))
  1919. PerformFold = isIndexInRangeOfSequentialType(STy, CI);
  1920. if (PerformFold) {
  1921. SmallVector<Value*, 16> NewIndices;
  1922. NewIndices.reserve(Idxs.size() + CE->getNumOperands());
  1923. NewIndices.append(CE->op_begin() + 1, CE->op_end() - 1);
  1924. // Add the last index of the source with the first index of the new GEP.
  1925. // Make sure to handle the case when they are actually different types.
  1926. Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
  1927. // Otherwise it must be an array.
  1928. if (!Idx0->isNullValue()) {
  1929. Type *IdxTy = Combined->getType();
  1930. if (IdxTy != Idx0->getType()) {
  1931. unsigned CommonExtendedWidth =
  1932. std::max(IdxTy->getIntegerBitWidth(),
  1933. Idx0->getType()->getIntegerBitWidth());
  1934. CommonExtendedWidth = std::max(CommonExtendedWidth, 64U);
  1935. Type *CommonTy =
  1936. Type::getIntNTy(IdxTy->getContext(), CommonExtendedWidth);
  1937. Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, CommonTy);
  1938. Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined, CommonTy);
  1939. Combined = ConstantExpr::get(Instruction::Add, C1, C2);
  1940. } else {
  1941. Combined =
  1942. ConstantExpr::get(Instruction::Add, Idx0, Combined);
  1943. }
  1944. }
  1945. NewIndices.push_back(Combined);
  1946. NewIndices.append(Idxs.begin() + 1, Idxs.end());
  1947. return ConstantExpr::getGetElementPtr(
  1948. cast<GEPOperator>(CE)->getSourceElementType(), CE->getOperand(0),
  1949. NewIndices, inBounds && cast<GEPOperator>(CE)->isInBounds());
  1950. }
  1951. }
  1952. // Attempt to fold casts to the same type away. For example, folding:
  1953. //
  1954. // i32* getelementptr ([2 x i32]* bitcast ([3 x i32]* %X to [2 x i32]*),
  1955. // i64 0, i64 0)
  1956. // into:
  1957. //
  1958. // i32* getelementptr ([3 x i32]* %X, i64 0, i64 0)
  1959. //
  1960. // Don't fold if the cast is changing address spaces.
  1961. if (CE->isCast() && Idxs.size() > 1 && Idx0->isNullValue()) {
  1962. PointerType *SrcPtrTy =
  1963. dyn_cast<PointerType>(CE->getOperand(0)->getType());
  1964. PointerType *DstPtrTy = dyn_cast<PointerType>(CE->getType());
  1965. if (SrcPtrTy && DstPtrTy) {
  1966. ArrayType *SrcArrayTy =
  1967. dyn_cast<ArrayType>(SrcPtrTy->getElementType());
  1968. ArrayType *DstArrayTy =
  1969. dyn_cast<ArrayType>(DstPtrTy->getElementType());
  1970. if (SrcArrayTy && DstArrayTy
  1971. && SrcArrayTy->getElementType() == DstArrayTy->getElementType()
  1972. && SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
  1973. return ConstantExpr::getGetElementPtr(
  1974. SrcArrayTy, (Constant *)CE->getOperand(0), Idxs, inBounds);
  1975. }
  1976. }
  1977. }
  1978. // Check to see if any array indices are not within the corresponding
  1979. // notional array or vector bounds. If so, try to determine if they can be
  1980. // factored out into preceding dimensions.
  1981. SmallVector<Constant *, 8> NewIdxs;
  1982. Type *Ty = PointeeTy;
  1983. Type *Prev = C->getType();
  1984. bool Unknown = !isa<ConstantInt>(Idxs[0]);
  1985. for (unsigned i = 1, e = Idxs.size(); i != e;
  1986. Prev = Ty, Ty = cast<CompositeType>(Ty)->getTypeAtIndex(Idxs[i]), ++i) {
  1987. if (ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) {
  1988. if (isa<ArrayType>(Ty) || isa<VectorType>(Ty))
  1989. if (CI->getSExtValue() > 0 &&
  1990. !isIndexInRangeOfSequentialType(cast<SequentialType>(Ty), CI)) {
  1991. if (isa<SequentialType>(Prev)) {
  1992. // It's out of range, but we can factor it into the prior
  1993. // dimension.
  1994. NewIdxs.resize(Idxs.size());
  1995. uint64_t NumElements = 0;
  1996. if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty))
  1997. NumElements = ATy->getNumElements();
  1998. else
  1999. NumElements = cast<VectorType>(Ty)->getNumElements();
  2000. ConstantInt *Factor = ConstantInt::get(CI->getType(), NumElements);
  2001. NewIdxs[i] = ConstantExpr::getSRem(CI, Factor);
  2002. Constant *PrevIdx = cast<Constant>(Idxs[i-1]);
  2003. Constant *Div = ConstantExpr::getSDiv(CI, Factor);
  2004. unsigned CommonExtendedWidth =
  2005. std::max(PrevIdx->getType()->getIntegerBitWidth(),
  2006. Div->getType()->getIntegerBitWidth());
  2007. CommonExtendedWidth = std::max(CommonExtendedWidth, 64U);
  2008. // Before adding, extend both operands to i64 to avoid
  2009. // overflow trouble.
  2010. if (!PrevIdx->getType()->isIntegerTy(CommonExtendedWidth))
  2011. PrevIdx = ConstantExpr::getSExt(
  2012. PrevIdx,
  2013. Type::getIntNTy(Div->getContext(), CommonExtendedWidth));
  2014. if (!Div->getType()->isIntegerTy(CommonExtendedWidth))
  2015. Div = ConstantExpr::getSExt(
  2016. Div, Type::getIntNTy(Div->getContext(), CommonExtendedWidth));
  2017. NewIdxs[i-1] = ConstantExpr::getAdd(PrevIdx, Div);
  2018. } else {
  2019. // It's out of range, but the prior dimension is a struct
  2020. // so we can't do anything about it.
  2021. Unknown = true;
  2022. }
  2023. }
  2024. } else {
  2025. // We don't know if it's in range or not.
  2026. Unknown = true;
  2027. }
  2028. }
  2029. // If we did any factoring, start over with the adjusted indices.
  2030. if (!NewIdxs.empty()) {
  2031. for (unsigned i = 0, e = Idxs.size(); i != e; ++i)
  2032. if (!NewIdxs[i]) NewIdxs[i] = cast<Constant>(Idxs[i]);
  2033. return ConstantExpr::getGetElementPtr(PointeeTy, C, NewIdxs, inBounds);
  2034. }
  2035. // If all indices are known integers and normalized, we can do a simple
  2036. // check for the "inbounds" property.
  2037. if (!Unknown && !inBounds)
  2038. if (auto *GV = dyn_cast<GlobalVariable>(C))
  2039. if (!GV->hasExternalWeakLinkage() && isInBoundsIndices(Idxs))
  2040. return ConstantExpr::getInBoundsGetElementPtr(PointeeTy, C, Idxs);
  2041. return nullptr;
  2042. }
  2043. Constant *llvm::ConstantFoldGetElementPtr(Constant *C,
  2044. bool inBounds,
  2045. ArrayRef<Constant *> Idxs) {
  2046. return ConstantFoldGetElementPtrImpl(
  2047. cast<PointerType>(C->getType()->getScalarType())->getElementType(), C,
  2048. inBounds, Idxs);
  2049. }
  2050. Constant *llvm::ConstantFoldGetElementPtr(Constant *C,
  2051. bool inBounds,
  2052. ArrayRef<Value *> Idxs) {
  2053. return ConstantFoldGetElementPtrImpl(
  2054. cast<PointerType>(C->getType()->getScalarType())->getElementType(), C,
  2055. inBounds, Idxs);
  2056. }
  2057. Constant *llvm::ConstantFoldGetElementPtr(Type *Ty, Constant *C,
  2058. bool inBounds,
  2059. ArrayRef<Constant *> Idxs) {
  2060. return ConstantFoldGetElementPtrImpl(Ty, C, inBounds, Idxs);
  2061. }
  2062. Constant *llvm::ConstantFoldGetElementPtr(Type *Ty, Constant *C,
  2063. bool inBounds,
  2064. ArrayRef<Value *> Idxs) {
  2065. return ConstantFoldGetElementPtrImpl(Ty, C, inBounds, Idxs);
  2066. }