ConstantFold.cpp 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264
  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. false && // HLSL change - not fold gep 0, ... 0 for HLSL.
  485. // Do not fold addrspacecast (gep 0, .., 0). It might make the
  486. // addrspacecast uncanonicalized.
  487. opc != Instruction::AddrSpaceCast) {
  488. // If all of the indexes in the GEP are null values, there is no pointer
  489. // adjustment going on. We might as well cast the source pointer.
  490. bool isAllNull = true;
  491. for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i)
  492. if (!CE->getOperand(i)->isNullValue()) {
  493. isAllNull = false;
  494. break;
  495. }
  496. if (isAllNull)
  497. // This is casting one pointer type to another, always BitCast
  498. return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy);
  499. }
  500. }
  501. // If the cast operand is a constant vector, perform the cast by
  502. // operating on each element. In the cast of bitcasts, the element
  503. // count may be mismatched; don't attempt to handle that here.
  504. if ((isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) &&
  505. DestTy->isVectorTy() &&
  506. DestTy->getVectorNumElements() == V->getType()->getVectorNumElements()) {
  507. SmallVector<Constant*, 16> res;
  508. VectorType *DestVecTy = cast<VectorType>(DestTy);
  509. Type *DstEltTy = DestVecTy->getElementType();
  510. Type *Ty = IntegerType::get(V->getContext(), 32);
  511. for (unsigned i = 0, e = V->getType()->getVectorNumElements(); i != e; ++i) {
  512. Constant *C =
  513. ConstantExpr::getExtractElement(V, ConstantInt::get(Ty, i));
  514. res.push_back(ConstantExpr::getCast(opc, C, DstEltTy));
  515. }
  516. return ConstantVector::get(res);
  517. }
  518. // We actually have to do a cast now. Perform the cast according to the
  519. // opcode specified.
  520. switch (opc) {
  521. default:
  522. llvm_unreachable("Failed to cast constant expression");
  523. case Instruction::FPTrunc:
  524. case Instruction::FPExt:
  525. if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
  526. bool ignored;
  527. APFloat Val = FPC->getValueAPF();
  528. Val.convert(DestTy->isHalfTy() ? APFloat::IEEEhalf :
  529. DestTy->isFloatTy() ? APFloat::IEEEsingle :
  530. DestTy->isDoubleTy() ? APFloat::IEEEdouble :
  531. DestTy->isX86_FP80Ty() ? APFloat::x87DoubleExtended :
  532. DestTy->isFP128Ty() ? APFloat::IEEEquad :
  533. DestTy->isPPC_FP128Ty() ? APFloat::PPCDoubleDouble :
  534. APFloat::Bogus,
  535. APFloat::rmNearestTiesToEven, &ignored);
  536. return ConstantFP::get(V->getContext(), Val);
  537. }
  538. return nullptr; // Can't fold.
  539. case Instruction::FPToUI:
  540. case Instruction::FPToSI:
  541. if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) {
  542. const APFloat &V = FPC->getValueAPF();
  543. bool ignored;
  544. uint64_t x[2];
  545. uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
  546. if (APFloat::opInvalidOp ==
  547. V.convertToInteger(x, DestBitWidth, opc==Instruction::FPToSI,
  548. APFloat::rmTowardZero, &ignored)) {
  549. // Undefined behavior invoked - the destination type can't represent
  550. // the input constant.
  551. return UndefValue::get(DestTy);
  552. }
  553. APInt Val(DestBitWidth, x);
  554. return ConstantInt::get(FPC->getContext(), Val);
  555. }
  556. return nullptr; // Can't fold.
  557. case Instruction::IntToPtr: //always treated as unsigned
  558. if (V->isNullValue()) // Is it an integral null value?
  559. return ConstantPointerNull::get(cast<PointerType>(DestTy));
  560. return nullptr; // Other pointer types cannot be casted
  561. case Instruction::PtrToInt: // always treated as unsigned
  562. // Is it a null pointer value?
  563. if (V->isNullValue())
  564. return ConstantInt::get(DestTy, 0);
  565. // If this is a sizeof-like expression, pull out multiplications by
  566. // known factors to expose them to subsequent folding. If it's an
  567. // alignof-like expression, factor out known factors.
  568. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
  569. if (CE->getOpcode() == Instruction::GetElementPtr &&
  570. CE->getOperand(0)->isNullValue()) {
  571. GEPOperator *GEPO = cast<GEPOperator>(CE);
  572. Type *Ty = GEPO->getSourceElementType();
  573. if (CE->getNumOperands() == 2) {
  574. // Handle a sizeof-like expression.
  575. Constant *Idx = CE->getOperand(1);
  576. bool isOne = isa<ConstantInt>(Idx) && cast<ConstantInt>(Idx)->isOne();
  577. if (Constant *C = getFoldedSizeOf(Ty, DestTy, !isOne)) {
  578. Idx = ConstantExpr::getCast(CastInst::getCastOpcode(Idx, true,
  579. DestTy, false),
  580. Idx, DestTy);
  581. return ConstantExpr::getMul(C, Idx);
  582. }
  583. } else if (CE->getNumOperands() == 3 &&
  584. CE->getOperand(1)->isNullValue()) {
  585. // Handle an alignof-like expression.
  586. if (StructType *STy = dyn_cast<StructType>(Ty))
  587. if (!STy->isPacked()) {
  588. ConstantInt *CI = cast<ConstantInt>(CE->getOperand(2));
  589. if (CI->isOne() &&
  590. STy->getNumElements() == 2 &&
  591. STy->getElementType(0)->isIntegerTy(1)) {
  592. return getFoldedAlignOf(STy->getElementType(1), DestTy, false);
  593. }
  594. }
  595. // Handle an offsetof-like expression.
  596. if (Ty->isStructTy() || Ty->isArrayTy()) {
  597. if (Constant *C = getFoldedOffsetOf(Ty, CE->getOperand(2),
  598. DestTy, false))
  599. return C;
  600. }
  601. }
  602. }
  603. // Other pointer types cannot be casted
  604. return nullptr;
  605. case Instruction::UIToFP:
  606. case Instruction::SIToFP:
  607. if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
  608. APInt api = CI->getValue();
  609. APFloat apf(DestTy->getFltSemantics(),
  610. APInt::getNullValue(DestTy->getPrimitiveSizeInBits()));
  611. if (APFloat::opOverflow &
  612. apf.convertFromAPInt(api, opc==Instruction::SIToFP,
  613. APFloat::rmNearestTiesToEven)) {
  614. // Undefined behavior invoked - the destination type can't represent
  615. // the input constant.
  616. return UndefValue::get(DestTy);
  617. }
  618. return ConstantFP::get(V->getContext(), apf);
  619. }
  620. return nullptr;
  621. case Instruction::ZExt:
  622. if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
  623. uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
  624. return ConstantInt::get(V->getContext(),
  625. CI->getValue().zext(BitWidth));
  626. }
  627. return nullptr;
  628. case Instruction::SExt:
  629. if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
  630. uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth();
  631. return ConstantInt::get(V->getContext(),
  632. CI->getValue().sext(BitWidth));
  633. }
  634. return nullptr;
  635. case Instruction::Trunc: {
  636. if (V->getType()->isVectorTy())
  637. return nullptr;
  638. uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth();
  639. if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
  640. return ConstantInt::get(V->getContext(),
  641. CI->getValue().trunc(DestBitWidth));
  642. }
  643. // The input must be a constantexpr. See if we can simplify this based on
  644. // the bytes we are demanding. Only do this if the source and dest are an
  645. // even multiple of a byte.
  646. if ((DestBitWidth & 7) == 0 &&
  647. (cast<IntegerType>(V->getType())->getBitWidth() & 7) == 0)
  648. if (Constant *Res = ExtractConstantBytes(V, 0, DestBitWidth / 8))
  649. return Res;
  650. return nullptr;
  651. }
  652. case Instruction::BitCast:
  653. return FoldBitCast(V, DestTy);
  654. case Instruction::AddrSpaceCast:
  655. return nullptr;
  656. }
  657. }
  658. Constant *llvm::ConstantFoldSelectInstruction(Constant *Cond,
  659. Constant *V1, Constant *V2) {
  660. // Check for i1 and vector true/false conditions.
  661. if (Cond->isNullValue()) return V2;
  662. if (Cond->isAllOnesValue()) return V1;
  663. // If the condition is a vector constant, fold the result elementwise.
  664. if (ConstantVector *CondV = dyn_cast<ConstantVector>(Cond)) {
  665. SmallVector<Constant*, 16> Result;
  666. Type *Ty = IntegerType::get(CondV->getContext(), 32);
  667. for (unsigned i = 0, e = V1->getType()->getVectorNumElements(); i != e;++i){
  668. Constant *V;
  669. Constant *V1Element = ConstantExpr::getExtractElement(V1,
  670. ConstantInt::get(Ty, i));
  671. Constant *V2Element = ConstantExpr::getExtractElement(V2,
  672. ConstantInt::get(Ty, i));
  673. Constant *Cond = dyn_cast<Constant>(CondV->getOperand(i));
  674. if (V1Element == V2Element) {
  675. V = V1Element;
  676. } else if (isa<UndefValue>(Cond)) {
  677. V = isa<UndefValue>(V1Element) ? V1Element : V2Element;
  678. } else {
  679. if (!isa<ConstantInt>(Cond)) break;
  680. V = Cond->isNullValue() ? V2Element : V1Element;
  681. }
  682. Result.push_back(V);
  683. }
  684. // If we were able to build the vector, return it.
  685. if (Result.size() == V1->getType()->getVectorNumElements())
  686. return ConstantVector::get(Result);
  687. }
  688. if (isa<UndefValue>(Cond)) {
  689. if (isa<UndefValue>(V1)) return V1;
  690. return V2;
  691. }
  692. if (isa<UndefValue>(V1)) return V2;
  693. if (isa<UndefValue>(V2)) return V1;
  694. if (V1 == V2) return V1;
  695. if (ConstantExpr *TrueVal = dyn_cast<ConstantExpr>(V1)) {
  696. if (TrueVal->getOpcode() == Instruction::Select)
  697. if (TrueVal->getOperand(0) == Cond)
  698. return ConstantExpr::getSelect(Cond, TrueVal->getOperand(1), V2);
  699. }
  700. if (ConstantExpr *FalseVal = dyn_cast<ConstantExpr>(V2)) {
  701. if (FalseVal->getOpcode() == Instruction::Select)
  702. if (FalseVal->getOperand(0) == Cond)
  703. return ConstantExpr::getSelect(Cond, V1, FalseVal->getOperand(2));
  704. }
  705. return nullptr;
  706. }
  707. Constant *llvm::ConstantFoldExtractElementInstruction(Constant *Val,
  708. Constant *Idx) {
  709. if (isa<UndefValue>(Val)) // ee(undef, x) -> undef
  710. return UndefValue::get(Val->getType()->getVectorElementType());
  711. if (Val->isNullValue()) // ee(zero, x) -> zero
  712. return Constant::getNullValue(Val->getType()->getVectorElementType());
  713. // ee({w,x,y,z}, undef) -> undef
  714. if (isa<UndefValue>(Idx))
  715. return UndefValue::get(Val->getType()->getVectorElementType());
  716. if (ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) {
  717. // ee({w,x,y,z}, wrong_value) -> undef
  718. if (CIdx->uge(Val->getType()->getVectorNumElements()))
  719. return UndefValue::get(Val->getType()->getVectorElementType());
  720. return Val->getAggregateElement(CIdx->getZExtValue());
  721. }
  722. return nullptr;
  723. }
  724. Constant *llvm::ConstantFoldInsertElementInstruction(Constant *Val,
  725. Constant *Elt,
  726. Constant *Idx) {
  727. if (isa<UndefValue>(Idx))
  728. return UndefValue::get(Val->getType());
  729. ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx);
  730. if (!CIdx) return nullptr;
  731. unsigned NumElts = Val->getType()->getVectorNumElements();
  732. if (CIdx->uge(NumElts))
  733. return UndefValue::get(Val->getType());
  734. SmallVector<Constant*, 16> Result;
  735. Result.reserve(NumElts);
  736. auto *Ty = Type::getInt32Ty(Val->getContext());
  737. uint64_t IdxVal = CIdx->getZExtValue();
  738. for (unsigned i = 0; i != NumElts; ++i) {
  739. if (i == IdxVal) {
  740. Result.push_back(Elt);
  741. continue;
  742. }
  743. Constant *C = ConstantExpr::getExtractElement(Val, ConstantInt::get(Ty, i));
  744. Result.push_back(C);
  745. }
  746. return ConstantVector::get(Result);
  747. }
  748. Constant *llvm::ConstantFoldShuffleVectorInstruction(Constant *V1,
  749. Constant *V2,
  750. Constant *Mask) {
  751. unsigned MaskNumElts = Mask->getType()->getVectorNumElements();
  752. Type *EltTy = V1->getType()->getVectorElementType();
  753. // Undefined shuffle mask -> undefined value.
  754. if (isa<UndefValue>(Mask))
  755. return UndefValue::get(VectorType::get(EltTy, MaskNumElts));
  756. // Don't break the bitcode reader hack.
  757. if (isa<ConstantExpr>(Mask)) return nullptr;
  758. unsigned SrcNumElts = V1->getType()->getVectorNumElements();
  759. // Loop over the shuffle mask, evaluating each element.
  760. SmallVector<Constant*, 32> Result;
  761. for (unsigned i = 0; i != MaskNumElts; ++i) {
  762. int Elt = ShuffleVectorInst::getMaskValue(Mask, i);
  763. if (Elt == -1) {
  764. Result.push_back(UndefValue::get(EltTy));
  765. continue;
  766. }
  767. Constant *InElt;
  768. if (unsigned(Elt) >= SrcNumElts*2)
  769. InElt = UndefValue::get(EltTy);
  770. else if (unsigned(Elt) >= SrcNumElts) {
  771. Type *Ty = IntegerType::get(V2->getContext(), 32);
  772. InElt =
  773. ConstantExpr::getExtractElement(V2,
  774. ConstantInt::get(Ty, Elt - SrcNumElts));
  775. } else {
  776. Type *Ty = IntegerType::get(V1->getContext(), 32);
  777. InElt = ConstantExpr::getExtractElement(V1, ConstantInt::get(Ty, Elt));
  778. }
  779. Result.push_back(InElt);
  780. }
  781. return ConstantVector::get(Result);
  782. }
  783. Constant *llvm::ConstantFoldExtractValueInstruction(Constant *Agg,
  784. ArrayRef<unsigned> Idxs) {
  785. // Base case: no indices, so return the entire value.
  786. if (Idxs.empty())
  787. return Agg;
  788. if (Constant *C = Agg->getAggregateElement(Idxs[0]))
  789. return ConstantFoldExtractValueInstruction(C, Idxs.slice(1));
  790. return nullptr;
  791. }
  792. Constant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg,
  793. Constant *Val,
  794. ArrayRef<unsigned> Idxs) {
  795. // Base case: no indices, so replace the entire value.
  796. if (Idxs.empty())
  797. return Val;
  798. unsigned NumElts;
  799. if (StructType *ST = dyn_cast<StructType>(Agg->getType()))
  800. NumElts = ST->getNumElements();
  801. else if (ArrayType *AT = dyn_cast<ArrayType>(Agg->getType()))
  802. NumElts = AT->getNumElements();
  803. else
  804. NumElts = Agg->getType()->getVectorNumElements();
  805. SmallVector<Constant*, 32> Result;
  806. for (unsigned i = 0; i != NumElts; ++i) {
  807. Constant *C = Agg->getAggregateElement(i);
  808. if (!C) return nullptr;
  809. if (Idxs[0] == i)
  810. C = ConstantFoldInsertValueInstruction(C, Val, Idxs.slice(1));
  811. Result.push_back(C);
  812. }
  813. if (StructType *ST = dyn_cast<StructType>(Agg->getType()))
  814. return ConstantStruct::get(ST, Result);
  815. if (ArrayType *AT = dyn_cast<ArrayType>(Agg->getType()))
  816. return ConstantArray::get(AT, Result);
  817. return ConstantVector::get(Result);
  818. }
  819. Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
  820. Constant *C1, Constant *C2) {
  821. // Handle UndefValue up front.
  822. if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
  823. switch (Opcode) {
  824. case Instruction::Xor:
  825. if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
  826. // Handle undef ^ undef -> 0 special case. This is a common
  827. // idiom (misuse).
  828. return Constant::getNullValue(C1->getType());
  829. // Fallthrough
  830. case Instruction::Add:
  831. case Instruction::Sub:
  832. return UndefValue::get(C1->getType());
  833. case Instruction::And:
  834. if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef & undef -> undef
  835. return C1;
  836. return Constant::getNullValue(C1->getType()); // undef & X -> 0
  837. case Instruction::Mul: {
  838. // undef * undef -> undef
  839. if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
  840. return C1;
  841. const APInt *CV;
  842. // X * undef -> undef if X is odd
  843. if (match(C1, m_APInt(CV)) || match(C2, m_APInt(CV)))
  844. if ((*CV)[0])
  845. return UndefValue::get(C1->getType());
  846. // X * undef -> 0 otherwise
  847. return Constant::getNullValue(C1->getType());
  848. }
  849. case Instruction::SDiv:
  850. case Instruction::UDiv:
  851. // X / undef -> undef
  852. if (match(C1, m_Zero()))
  853. return C2;
  854. // undef / 0 -> undef
  855. // undef / 1 -> undef
  856. if (match(C2, m_Zero()) || match(C2, m_One()))
  857. return C1;
  858. // undef / X -> 0 otherwise
  859. return Constant::getNullValue(C1->getType());
  860. case Instruction::URem:
  861. case Instruction::SRem:
  862. // X % undef -> undef
  863. if (match(C2, m_Undef()))
  864. return C2;
  865. // undef % 0 -> undef
  866. if (match(C2, m_Zero()))
  867. return C1;
  868. // undef % X -> 0 otherwise
  869. return Constant::getNullValue(C1->getType());
  870. case Instruction::Or: // X | undef -> -1
  871. if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef | undef -> undef
  872. return C1;
  873. return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0
  874. case Instruction::LShr:
  875. // X >>l undef -> undef
  876. if (isa<UndefValue>(C2))
  877. return C2;
  878. // undef >>l 0 -> undef
  879. if (match(C2, m_Zero()))
  880. return C1;
  881. // undef >>l X -> 0
  882. return Constant::getNullValue(C1->getType());
  883. case Instruction::AShr:
  884. // X >>a undef -> undef
  885. if (isa<UndefValue>(C2))
  886. return C2;
  887. // undef >>a 0 -> undef
  888. if (match(C2, m_Zero()))
  889. return C1;
  890. // TODO: undef >>a X -> undef if the shift is exact
  891. // undef >>a X -> 0
  892. return Constant::getNullValue(C1->getType());
  893. case Instruction::Shl:
  894. // X << undef -> undef
  895. if (isa<UndefValue>(C2))
  896. return C2;
  897. // undef << 0 -> undef
  898. if (match(C2, m_Zero()))
  899. return C1;
  900. // undef << X -> 0
  901. return Constant::getNullValue(C1->getType());
  902. }
  903. }
  904. // Handle simplifications when the RHS is a constant int.
  905. if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
  906. switch (Opcode) {
  907. case Instruction::Add:
  908. if (CI2->equalsInt(0)) return C1; // X + 0 == X
  909. break;
  910. case Instruction::Sub:
  911. if (CI2->equalsInt(0)) return C1; // X - 0 == X
  912. break;
  913. case Instruction::Mul:
  914. if (CI2->equalsInt(0)) return C2; // X * 0 == 0
  915. if (CI2->equalsInt(1))
  916. return C1; // X * 1 == X
  917. break;
  918. case Instruction::UDiv:
  919. case Instruction::SDiv:
  920. if (CI2->equalsInt(1))
  921. return C1; // X / 1 == X
  922. if (CI2->equalsInt(0))
  923. return UndefValue::get(CI2->getType()); // X / 0 == undef
  924. break;
  925. case Instruction::URem:
  926. case Instruction::SRem:
  927. if (CI2->equalsInt(1))
  928. return Constant::getNullValue(CI2->getType()); // X % 1 == 0
  929. if (CI2->equalsInt(0))
  930. return UndefValue::get(CI2->getType()); // X % 0 == undef
  931. break;
  932. case Instruction::And:
  933. if (CI2->isZero()) return C2; // X & 0 == 0
  934. if (CI2->isAllOnesValue())
  935. return C1; // X & -1 == X
  936. if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
  937. // (zext i32 to i64) & 4294967295 -> (zext i32 to i64)
  938. if (CE1->getOpcode() == Instruction::ZExt) {
  939. unsigned DstWidth = CI2->getType()->getBitWidth();
  940. unsigned SrcWidth =
  941. CE1->getOperand(0)->getType()->getPrimitiveSizeInBits();
  942. APInt PossiblySetBits(APInt::getLowBitsSet(DstWidth, SrcWidth));
  943. if ((PossiblySetBits & CI2->getValue()) == PossiblySetBits)
  944. return C1;
  945. }
  946. // If and'ing the address of a global with a constant, fold it.
  947. if (CE1->getOpcode() == Instruction::PtrToInt &&
  948. isa<GlobalValue>(CE1->getOperand(0))) {
  949. GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0));
  950. // Functions are at least 4-byte aligned.
  951. unsigned GVAlign = GV->getAlignment();
  952. if (isa<Function>(GV))
  953. GVAlign = std::max(GVAlign, 4U);
  954. if (GVAlign > 1) {
  955. unsigned DstWidth = CI2->getType()->getBitWidth();
  956. unsigned SrcWidth = std::min(DstWidth, Log2_32(GVAlign));
  957. APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth));
  958. // If checking bits we know are clear, return zero.
  959. if ((CI2->getValue() & BitsNotSet) == CI2->getValue())
  960. return Constant::getNullValue(CI2->getType());
  961. }
  962. }
  963. }
  964. break;
  965. case Instruction::Or:
  966. if (CI2->equalsInt(0)) return C1; // X | 0 == X
  967. if (CI2->isAllOnesValue())
  968. return C2; // X | -1 == -1
  969. break;
  970. case Instruction::Xor:
  971. if (CI2->equalsInt(0)) return C1; // X ^ 0 == X
  972. if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
  973. switch (CE1->getOpcode()) {
  974. default: break;
  975. case Instruction::ICmp:
  976. case Instruction::FCmp:
  977. // cmp pred ^ true -> cmp !pred
  978. assert(CI2->equalsInt(1));
  979. CmpInst::Predicate pred = (CmpInst::Predicate)CE1->getPredicate();
  980. pred = CmpInst::getInversePredicate(pred);
  981. return ConstantExpr::getCompare(pred, CE1->getOperand(0),
  982. CE1->getOperand(1));
  983. }
  984. }
  985. break;
  986. case Instruction::AShr:
  987. // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2
  988. if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1))
  989. if (CE1->getOpcode() == Instruction::ZExt) // Top bits known zero.
  990. return ConstantExpr::getLShr(C1, C2);
  991. break;
  992. }
  993. } else if (isa<ConstantInt>(C1)) {
  994. // If C1 is a ConstantInt and C2 is not, swap the operands.
  995. if (Instruction::isCommutative(Opcode))
  996. return ConstantExpr::get(Opcode, C2, C1);
  997. }
  998. // At this point we know neither constant is an UndefValue.
  999. if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) {
  1000. if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) {
  1001. const APInt &C1V = CI1->getValue();
  1002. const APInt &C2V = CI2->getValue();
  1003. switch (Opcode) {
  1004. default:
  1005. break;
  1006. case Instruction::Add:
  1007. return ConstantInt::get(CI1->getContext(), C1V + C2V);
  1008. case Instruction::Sub:
  1009. return ConstantInt::get(CI1->getContext(), C1V - C2V);
  1010. case Instruction::Mul:
  1011. return ConstantInt::get(CI1->getContext(), C1V * C2V);
  1012. case Instruction::UDiv:
  1013. assert(!CI2->isNullValue() && "Div by zero handled above");
  1014. return ConstantInt::get(CI1->getContext(), C1V.udiv(C2V));
  1015. case Instruction::SDiv:
  1016. assert(!CI2->isNullValue() && "Div by zero handled above");
  1017. if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
  1018. return UndefValue::get(CI1->getType()); // MIN_INT / -1 -> undef
  1019. return ConstantInt::get(CI1->getContext(), C1V.sdiv(C2V));
  1020. case Instruction::URem:
  1021. assert(!CI2->isNullValue() && "Div by zero handled above");
  1022. return ConstantInt::get(CI1->getContext(), C1V.urem(C2V));
  1023. case Instruction::SRem:
  1024. assert(!CI2->isNullValue() && "Div by zero handled above");
  1025. if (C2V.isAllOnesValue() && C1V.isMinSignedValue())
  1026. return UndefValue::get(CI1->getType()); // MIN_INT % -1 -> undef
  1027. return ConstantInt::get(CI1->getContext(), C1V.srem(C2V));
  1028. case Instruction::And:
  1029. return ConstantInt::get(CI1->getContext(), C1V & C2V);
  1030. case Instruction::Or:
  1031. return ConstantInt::get(CI1->getContext(), C1V | C2V);
  1032. case Instruction::Xor:
  1033. return ConstantInt::get(CI1->getContext(), C1V ^ C2V);
  1034. case Instruction::Shl:
  1035. if (C2V.ult(C1V.getBitWidth()))
  1036. return ConstantInt::get(CI1->getContext(), C1V.shl(C2V));
  1037. return UndefValue::get(C1->getType()); // too big shift is undef
  1038. case Instruction::LShr:
  1039. if (C2V.ult(C1V.getBitWidth()))
  1040. return ConstantInt::get(CI1->getContext(), C1V.lshr(C2V));
  1041. return UndefValue::get(C1->getType()); // too big shift is undef
  1042. case Instruction::AShr:
  1043. if (C2V.ult(C1V.getBitWidth()))
  1044. return ConstantInt::get(CI1->getContext(), C1V.ashr(C2V));
  1045. return UndefValue::get(C1->getType()); // too big shift is undef
  1046. }
  1047. }
  1048. switch (Opcode) {
  1049. case Instruction::SDiv:
  1050. case Instruction::UDiv:
  1051. case Instruction::URem:
  1052. case Instruction::SRem:
  1053. case Instruction::LShr:
  1054. case Instruction::AShr:
  1055. case Instruction::Shl:
  1056. if (CI1->equalsInt(0)) return C1;
  1057. break;
  1058. default:
  1059. break;
  1060. }
  1061. } else if (ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) {
  1062. if (ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) {
  1063. APFloat C1V = CFP1->getValueAPF();
  1064. APFloat C2V = CFP2->getValueAPF();
  1065. APFloat C3V = C1V; // copy for modification
  1066. switch (Opcode) {
  1067. default:
  1068. break;
  1069. case Instruction::FAdd:
  1070. (void)C3V.add(C2V, APFloat::rmNearestTiesToEven);
  1071. return ConstantFP::get(C1->getContext(), C3V);
  1072. case Instruction::FSub:
  1073. (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven);
  1074. return ConstantFP::get(C1->getContext(), C3V);
  1075. case Instruction::FMul:
  1076. (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven);
  1077. return ConstantFP::get(C1->getContext(), C3V);
  1078. case Instruction::FDiv:
  1079. (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven);
  1080. return ConstantFP::get(C1->getContext(), C3V);
  1081. case Instruction::FRem:
  1082. (void)C3V.mod(C2V, APFloat::rmNearestTiesToEven);
  1083. return ConstantFP::get(C1->getContext(), C3V);
  1084. }
  1085. }
  1086. } else if (VectorType *VTy = dyn_cast<VectorType>(C1->getType())) {
  1087. // Perform elementwise folding.
  1088. SmallVector<Constant*, 16> Result;
  1089. Type *Ty = IntegerType::get(VTy->getContext(), 32);
  1090. for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
  1091. Constant *LHS =
  1092. ConstantExpr::getExtractElement(C1, ConstantInt::get(Ty, i));
  1093. Constant *RHS =
  1094. ConstantExpr::getExtractElement(C2, ConstantInt::get(Ty, i));
  1095. Result.push_back(ConstantExpr::get(Opcode, LHS, RHS));
  1096. }
  1097. return ConstantVector::get(Result);
  1098. }
  1099. if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
  1100. // There are many possible foldings we could do here. We should probably
  1101. // at least fold add of a pointer with an integer into the appropriate
  1102. // getelementptr. This will improve alias analysis a bit.
  1103. // Given ((a + b) + c), if (b + c) folds to something interesting, return
  1104. // (a + (b + c)).
  1105. if (Instruction::isAssociative(Opcode) && CE1->getOpcode() == Opcode) {
  1106. Constant *T = ConstantExpr::get(Opcode, CE1->getOperand(1), C2);
  1107. if (!isa<ConstantExpr>(T) || cast<ConstantExpr>(T)->getOpcode() != Opcode)
  1108. return ConstantExpr::get(Opcode, CE1->getOperand(0), T);
  1109. }
  1110. } else if (isa<ConstantExpr>(C2)) {
  1111. // If C2 is a constant expr and C1 isn't, flop them around and fold the
  1112. // other way if possible.
  1113. if (Instruction::isCommutative(Opcode))
  1114. return ConstantFoldBinaryInstruction(Opcode, C2, C1);
  1115. }
  1116. // i1 can be simplified in many cases.
  1117. if (C1->getType()->isIntegerTy(1)) {
  1118. switch (Opcode) {
  1119. case Instruction::Add:
  1120. case Instruction::Sub:
  1121. return ConstantExpr::getXor(C1, C2);
  1122. case Instruction::Mul:
  1123. return ConstantExpr::getAnd(C1, C2);
  1124. case Instruction::Shl:
  1125. case Instruction::LShr:
  1126. case Instruction::AShr:
  1127. // We can assume that C2 == 0. If it were one the result would be
  1128. // undefined because the shift value is as large as the bitwidth.
  1129. return C1;
  1130. case Instruction::SDiv:
  1131. case Instruction::UDiv:
  1132. // We can assume that C2 == 1. If it were zero the result would be
  1133. // undefined through division by zero.
  1134. return C1;
  1135. case Instruction::URem:
  1136. case Instruction::SRem:
  1137. // We can assume that C2 == 1. If it were zero the result would be
  1138. // undefined through division by zero.
  1139. return ConstantInt::getFalse(C1->getContext());
  1140. default:
  1141. break;
  1142. }
  1143. }
  1144. // We don't know how to fold this.
  1145. return nullptr;
  1146. }
  1147. /// isZeroSizedType - This type is zero sized if its an array or structure of
  1148. /// zero sized types. The only leaf zero sized type is an empty structure.
  1149. static bool isMaybeZeroSizedType(Type *Ty) {
  1150. if (StructType *STy = dyn_cast<StructType>(Ty)) {
  1151. if (STy->isOpaque()) return true; // Can't say.
  1152. // If all of elements have zero size, this does too.
  1153. for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
  1154. if (!isMaybeZeroSizedType(STy->getElementType(i))) return false;
  1155. return true;
  1156. } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
  1157. return isMaybeZeroSizedType(ATy->getElementType());
  1158. }
  1159. return false;
  1160. }
  1161. /// IdxCompare - Compare the two constants as though they were getelementptr
  1162. /// indices. This allows coersion of the types to be the same thing.
  1163. ///
  1164. /// If the two constants are the "same" (after coersion), return 0. If the
  1165. /// first is less than the second, return -1, if the second is less than the
  1166. /// first, return 1. If the constants are not integral, return -2.
  1167. ///
  1168. static int IdxCompare(Constant *C1, Constant *C2, Type *ElTy) {
  1169. if (C1 == C2) return 0;
  1170. // Ok, we found a different index. If they are not ConstantInt, we can't do
  1171. // anything with them.
  1172. if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2))
  1173. return -2; // don't know!
  1174. // We cannot compare the indices if they don't fit in an int64_t.
  1175. if (cast<ConstantInt>(C1)->getValue().getActiveBits() > 64 ||
  1176. cast<ConstantInt>(C2)->getValue().getActiveBits() > 64)
  1177. return -2; // don't know!
  1178. // Ok, we have two differing integer indices. Sign extend them to be the same
  1179. // type.
  1180. int64_t C1Val = cast<ConstantInt>(C1)->getSExtValue();
  1181. int64_t C2Val = cast<ConstantInt>(C2)->getSExtValue();
  1182. if (C1Val == C2Val) return 0; // They are equal
  1183. // If the type being indexed over is really just a zero sized type, there is
  1184. // no pointer difference being made here.
  1185. if (isMaybeZeroSizedType(ElTy))
  1186. return -2; // dunno.
  1187. // If they are really different, now that they are the same type, then we
  1188. // found a difference!
  1189. if (C1Val < C2Val)
  1190. return -1;
  1191. else
  1192. return 1;
  1193. }
  1194. /// evaluateFCmpRelation - This function determines if there is anything we can
  1195. /// decide about the two constants provided. This doesn't need to handle simple
  1196. /// things like ConstantFP comparisons, but should instead handle ConstantExprs.
  1197. /// If we can determine that the two constants have a particular relation to
  1198. /// each other, we should return the corresponding FCmpInst predicate,
  1199. /// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in
  1200. /// ConstantFoldCompareInstruction.
  1201. ///
  1202. /// To simplify this code we canonicalize the relation so that the first
  1203. /// operand is always the most "complex" of the two. We consider ConstantFP
  1204. /// to be the simplest, and ConstantExprs to be the most complex.
  1205. static FCmpInst::Predicate evaluateFCmpRelation(Constant *V1, Constant *V2) {
  1206. assert(V1->getType() == V2->getType() &&
  1207. "Cannot compare values of different types!");
  1208. // Handle degenerate case quickly
  1209. if (V1 == V2) return FCmpInst::FCMP_OEQ;
  1210. if (!isa<ConstantExpr>(V1)) {
  1211. if (!isa<ConstantExpr>(V2)) {
  1212. // Simple case, use the standard constant folder.
  1213. ConstantInt *R = nullptr;
  1214. R = dyn_cast<ConstantInt>(
  1215. ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, V1, V2));
  1216. if (R && !R->isZero())
  1217. return FCmpInst::FCMP_OEQ;
  1218. R = dyn_cast<ConstantInt>(
  1219. ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, V1, V2));
  1220. if (R && !R->isZero())
  1221. return FCmpInst::FCMP_OLT;
  1222. R = dyn_cast<ConstantInt>(
  1223. ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, V1, V2));
  1224. if (R && !R->isZero())
  1225. return FCmpInst::FCMP_OGT;
  1226. // Nothing more we can do
  1227. return FCmpInst::BAD_FCMP_PREDICATE;
  1228. }
  1229. // If the first operand is simple and second is ConstantExpr, swap operands.
  1230. FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(V2, V1);
  1231. if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE)
  1232. return FCmpInst::getSwappedPredicate(SwappedRelation);
  1233. } else {
  1234. // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
  1235. // constantexpr or a simple constant.
  1236. ConstantExpr *CE1 = cast<ConstantExpr>(V1);
  1237. switch (CE1->getOpcode()) {
  1238. case Instruction::FPTrunc:
  1239. case Instruction::FPExt:
  1240. case Instruction::UIToFP:
  1241. case Instruction::SIToFP:
  1242. // We might be able to do something with these but we don't right now.
  1243. break;
  1244. default:
  1245. break;
  1246. }
  1247. }
  1248. // There are MANY other foldings that we could perform here. They will
  1249. // probably be added on demand, as they seem needed.
  1250. return FCmpInst::BAD_FCMP_PREDICATE;
  1251. }
  1252. static ICmpInst::Predicate areGlobalsPotentiallyEqual(const GlobalValue *GV1,
  1253. const GlobalValue *GV2) {
  1254. auto isGlobalUnsafeForEquality = [](const GlobalValue *GV) {
  1255. if (GV->hasExternalWeakLinkage() || GV->hasWeakAnyLinkage())
  1256. return true;
  1257. if (const auto *GVar = dyn_cast<GlobalVariable>(GV)) {
  1258. Type *Ty = GVar->getValueType();
  1259. // A global with opaque type might end up being zero sized.
  1260. if (!Ty->isSized())
  1261. return true;
  1262. // A global with an empty type might lie at the address of any other
  1263. // global.
  1264. if (Ty->isEmptyTy())
  1265. return true;
  1266. }
  1267. return false;
  1268. };
  1269. // Don't try to decide equality of aliases.
  1270. if (!isa<GlobalAlias>(GV1) && !isa<GlobalAlias>(GV2))
  1271. if (!isGlobalUnsafeForEquality(GV1) && !isGlobalUnsafeForEquality(GV2))
  1272. return ICmpInst::ICMP_NE;
  1273. return ICmpInst::BAD_ICMP_PREDICATE;
  1274. }
  1275. /// evaluateICmpRelation - This function determines if there is anything we can
  1276. /// decide about the two constants provided. This doesn't need to handle simple
  1277. /// things like integer comparisons, but should instead handle ConstantExprs
  1278. /// and GlobalValues. If we can determine that the two constants have a
  1279. /// particular relation to each other, we should return the corresponding ICmp
  1280. /// predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE.
  1281. ///
  1282. /// To simplify this code we canonicalize the relation so that the first
  1283. /// operand is always the most "complex" of the two. We consider simple
  1284. /// constants (like ConstantInt) to be the simplest, followed by
  1285. /// GlobalValues, followed by ConstantExpr's (the most complex).
  1286. ///
  1287. static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2,
  1288. bool isSigned) {
  1289. assert(V1->getType() == V2->getType() &&
  1290. "Cannot compare different types of values!");
  1291. if (V1 == V2) return ICmpInst::ICMP_EQ;
  1292. if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1) &&
  1293. !isa<BlockAddress>(V1)) {
  1294. if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2) &&
  1295. !isa<BlockAddress>(V2)) {
  1296. // We distilled this down to a simple case, use the standard constant
  1297. // folder.
  1298. ConstantInt *R = nullptr;
  1299. ICmpInst::Predicate pred = ICmpInst::ICMP_EQ;
  1300. R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
  1301. if (R && !R->isZero())
  1302. return pred;
  1303. pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
  1304. R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
  1305. if (R && !R->isZero())
  1306. return pred;
  1307. pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
  1308. R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2));
  1309. if (R && !R->isZero())
  1310. return pred;
  1311. // If we couldn't figure it out, bail.
  1312. return ICmpInst::BAD_ICMP_PREDICATE;
  1313. }
  1314. // If the first operand is simple, swap operands.
  1315. ICmpInst::Predicate SwappedRelation =
  1316. evaluateICmpRelation(V2, V1, isSigned);
  1317. if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
  1318. return ICmpInst::getSwappedPredicate(SwappedRelation);
  1319. } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
  1320. if (isa<ConstantExpr>(V2)) { // Swap as necessary.
  1321. ICmpInst::Predicate SwappedRelation =
  1322. evaluateICmpRelation(V2, V1, isSigned);
  1323. if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
  1324. return ICmpInst::getSwappedPredicate(SwappedRelation);
  1325. return ICmpInst::BAD_ICMP_PREDICATE;
  1326. }
  1327. // Now we know that the RHS is a GlobalValue, BlockAddress or simple
  1328. // constant (which, since the types must match, means that it's a
  1329. // ConstantPointerNull).
  1330. if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
  1331. return areGlobalsPotentiallyEqual(GV, GV2);
  1332. } else if (isa<BlockAddress>(V2)) {
  1333. return ICmpInst::ICMP_NE; // Globals never equal labels.
  1334. } else {
  1335. assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
  1336. // GlobalVals can never be null unless they have external weak linkage.
  1337. // We don't try to evaluate aliases here.
  1338. if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV))
  1339. return ICmpInst::ICMP_NE;
  1340. }
  1341. } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
  1342. if (isa<ConstantExpr>(V2)) { // Swap as necessary.
  1343. ICmpInst::Predicate SwappedRelation =
  1344. evaluateICmpRelation(V2, V1, isSigned);
  1345. if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE)
  1346. return ICmpInst::getSwappedPredicate(SwappedRelation);
  1347. return ICmpInst::BAD_ICMP_PREDICATE;
  1348. }
  1349. // Now we know that the RHS is a GlobalValue, BlockAddress or simple
  1350. // constant (which, since the types must match, means that it is a
  1351. // ConstantPointerNull).
  1352. if (const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
  1353. // Block address in another function can't equal this one, but block
  1354. // addresses in the current function might be the same if blocks are
  1355. // empty.
  1356. if (BA2->getFunction() != BA->getFunction())
  1357. return ICmpInst::ICMP_NE;
  1358. } else {
  1359. // Block addresses aren't null, don't equal the address of globals.
  1360. assert((isa<ConstantPointerNull>(V2) || isa<GlobalValue>(V2)) &&
  1361. "Canonicalization guarantee!");
  1362. return ICmpInst::ICMP_NE;
  1363. }
  1364. } else {
  1365. // Ok, the LHS is known to be a constantexpr. The RHS can be any of a
  1366. // constantexpr, a global, block address, or a simple constant.
  1367. ConstantExpr *CE1 = cast<ConstantExpr>(V1);
  1368. Constant *CE1Op0 = CE1->getOperand(0);
  1369. switch (CE1->getOpcode()) {
  1370. case Instruction::Trunc:
  1371. case Instruction::FPTrunc:
  1372. case Instruction::FPExt:
  1373. case Instruction::FPToUI:
  1374. case Instruction::FPToSI:
  1375. break; // We can't evaluate floating point casts or truncations.
  1376. case Instruction::UIToFP:
  1377. case Instruction::SIToFP:
  1378. case Instruction::BitCast:
  1379. case Instruction::ZExt:
  1380. case Instruction::SExt:
  1381. // If the cast is not actually changing bits, and the second operand is a
  1382. // null pointer, do the comparison with the pre-casted value.
  1383. if (V2->isNullValue() &&
  1384. (CE1->getType()->isPointerTy() || CE1->getType()->isIntegerTy())) {
  1385. if (CE1->getOpcode() == Instruction::ZExt) isSigned = false;
  1386. if (CE1->getOpcode() == Instruction::SExt) isSigned = true;
  1387. return evaluateICmpRelation(CE1Op0,
  1388. Constant::getNullValue(CE1Op0->getType()),
  1389. isSigned);
  1390. }
  1391. break;
  1392. case Instruction::GetElementPtr: {
  1393. GEPOperator *CE1GEP = cast<GEPOperator>(CE1);
  1394. // Ok, since this is a getelementptr, we know that the constant has a
  1395. // pointer type. Check the various cases.
  1396. if (isa<ConstantPointerNull>(V2)) {
  1397. // If we are comparing a GEP to a null pointer, check to see if the base
  1398. // of the GEP equals the null pointer.
  1399. if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
  1400. if (GV->hasExternalWeakLinkage())
  1401. // Weak linkage GVals could be zero or not. We're comparing that
  1402. // to null pointer so its greater-or-equal
  1403. return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
  1404. else
  1405. // If its not weak linkage, the GVal must have a non-zero address
  1406. // so the result is greater-than
  1407. return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
  1408. } else if (isa<ConstantPointerNull>(CE1Op0)) {
  1409. // If we are indexing from a null pointer, check to see if we have any
  1410. // non-zero indices.
  1411. for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i)
  1412. if (!CE1->getOperand(i)->isNullValue())
  1413. // Offsetting from null, must not be equal.
  1414. return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
  1415. // Only zero indexes from null, must still be zero.
  1416. return ICmpInst::ICMP_EQ;
  1417. }
  1418. // Otherwise, we can't really say if the first operand is null or not.
  1419. } else if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
  1420. if (isa<ConstantPointerNull>(CE1Op0)) {
  1421. if (GV2->hasExternalWeakLinkage())
  1422. // Weak linkage GVals could be zero or not. We're comparing it to
  1423. // a null pointer, so its less-or-equal
  1424. return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
  1425. else
  1426. // If its not weak linkage, the GVal must have a non-zero address
  1427. // so the result is less-than
  1428. return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
  1429. } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) {
  1430. if (GV == GV2) {
  1431. // If this is a getelementptr of the same global, then it must be
  1432. // different. Because the types must match, the getelementptr could
  1433. // only have at most one index, and because we fold getelementptr's
  1434. // with a single zero index, it must be nonzero.
  1435. assert(CE1->getNumOperands() == 2 &&
  1436. !CE1->getOperand(1)->isNullValue() &&
  1437. "Surprising getelementptr!");
  1438. return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
  1439. } else {
  1440. if (CE1GEP->hasAllZeroIndices())
  1441. return areGlobalsPotentiallyEqual(GV, GV2);
  1442. return ICmpInst::BAD_ICMP_PREDICATE;
  1443. }
  1444. }
  1445. } else {
  1446. ConstantExpr *CE2 = cast<ConstantExpr>(V2);
  1447. Constant *CE2Op0 = CE2->getOperand(0);
  1448. // There are MANY other foldings that we could perform here. They will
  1449. // probably be added on demand, as they seem needed.
  1450. switch (CE2->getOpcode()) {
  1451. default: break;
  1452. case Instruction::GetElementPtr:
  1453. // By far the most common case to handle is when the base pointers are
  1454. // obviously to the same global.
  1455. if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) {
  1456. // Don't know relative ordering, but check for inequality.
  1457. if (CE1Op0 != CE2Op0) {
  1458. GEPOperator *CE2GEP = cast<GEPOperator>(CE2);
  1459. if (CE1GEP->hasAllZeroIndices() && CE2GEP->hasAllZeroIndices())
  1460. return areGlobalsPotentiallyEqual(cast<GlobalValue>(CE1Op0),
  1461. cast<GlobalValue>(CE2Op0));
  1462. return ICmpInst::BAD_ICMP_PREDICATE;
  1463. }
  1464. // Ok, we know that both getelementptr instructions are based on the
  1465. // same global. From this, we can precisely determine the relative
  1466. // ordering of the resultant pointers.
  1467. unsigned i = 1;
  1468. // The logic below assumes that the result of the comparison
  1469. // can be determined by finding the first index that differs.
  1470. // This doesn't work if there is over-indexing in any
  1471. // subsequent indices, so check for that case first.
  1472. if (!CE1->isGEPWithNoNotionalOverIndexing() ||
  1473. !CE2->isGEPWithNoNotionalOverIndexing())
  1474. return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
  1475. // Compare all of the operands the GEP's have in common.
  1476. gep_type_iterator GTI = gep_type_begin(CE1);
  1477. for (;i != CE1->getNumOperands() && i != CE2->getNumOperands();
  1478. ++i, ++GTI)
  1479. switch (IdxCompare(CE1->getOperand(i),
  1480. CE2->getOperand(i), GTI.getIndexedType())) {
  1481. case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT;
  1482. case 1: return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT;
  1483. case -2: return ICmpInst::BAD_ICMP_PREDICATE;
  1484. }
  1485. // Ok, we ran out of things they have in common. If any leftovers
  1486. // are non-zero then we have a difference, otherwise we are equal.
  1487. for (; i < CE1->getNumOperands(); ++i)
  1488. if (!CE1->getOperand(i)->isNullValue()) {
  1489. if (isa<ConstantInt>(CE1->getOperand(i)))
  1490. return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
  1491. else
  1492. return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
  1493. }
  1494. for (; i < CE2->getNumOperands(); ++i)
  1495. if (!CE2->getOperand(i)->isNullValue()) {
  1496. if (isa<ConstantInt>(CE2->getOperand(i)))
  1497. return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
  1498. else
  1499. return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal.
  1500. }
  1501. return ICmpInst::ICMP_EQ;
  1502. }
  1503. }
  1504. }
  1505. }
  1506. default:
  1507. break;
  1508. }
  1509. }
  1510. return ICmpInst::BAD_ICMP_PREDICATE;
  1511. }
  1512. Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred,
  1513. Constant *C1, Constant *C2) {
  1514. Type *ResultTy;
  1515. if (VectorType *VT = dyn_cast<VectorType>(C1->getType()))
  1516. ResultTy = VectorType::get(Type::getInt1Ty(C1->getContext()),
  1517. VT->getNumElements());
  1518. else
  1519. ResultTy = Type::getInt1Ty(C1->getContext());
  1520. // Fold FCMP_FALSE/FCMP_TRUE unconditionally.
  1521. if (pred == FCmpInst::FCMP_FALSE)
  1522. return Constant::getNullValue(ResultTy);
  1523. if (pred == FCmpInst::FCMP_TRUE)
  1524. return Constant::getAllOnesValue(ResultTy);
  1525. // Handle some degenerate cases first
  1526. if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) {
  1527. CmpInst::Predicate Predicate = CmpInst::Predicate(pred);
  1528. bool isIntegerPredicate = ICmpInst::isIntPredicate(Predicate);
  1529. // For EQ and NE, we can always pick a value for the undef to make the
  1530. // predicate pass or fail, so we can return undef.
  1531. // Also, if both operands are undef, we can return undef for int comparison.
  1532. if (ICmpInst::isEquality(Predicate) || (isIntegerPredicate && C1 == C2))
  1533. return UndefValue::get(ResultTy);
  1534. // Otherwise, for integer compare, pick the same value as the non-undef
  1535. // operand, and fold it to true or false.
  1536. if (isIntegerPredicate)
  1537. return ConstantInt::get(ResultTy, CmpInst::isTrueWhenEqual(pred));
  1538. // Choosing NaN for the undef will always make unordered comparison succeed
  1539. // and ordered comparison fails.
  1540. return ConstantInt::get(ResultTy, CmpInst::isUnordered(Predicate));
  1541. }
  1542. // icmp eq/ne(null,GV) -> false/true
  1543. if (C1->isNullValue()) {
  1544. if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
  1545. // Don't try to evaluate aliases. External weak GV can be null.
  1546. if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
  1547. if (pred == ICmpInst::ICMP_EQ)
  1548. return ConstantInt::getFalse(C1->getContext());
  1549. else if (pred == ICmpInst::ICMP_NE)
  1550. return ConstantInt::getTrue(C1->getContext());
  1551. }
  1552. // icmp eq/ne(GV,null) -> false/true
  1553. } else if (C2->isNullValue()) {
  1554. if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1))
  1555. // Don't try to evaluate aliases. External weak GV can be null.
  1556. if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) {
  1557. if (pred == ICmpInst::ICMP_EQ)
  1558. return ConstantInt::getFalse(C1->getContext());
  1559. else if (pred == ICmpInst::ICMP_NE)
  1560. return ConstantInt::getTrue(C1->getContext());
  1561. }
  1562. }
  1563. // If the comparison is a comparison between two i1's, simplify it.
  1564. if (C1->getType()->isIntegerTy(1)) {
  1565. switch(pred) {
  1566. case ICmpInst::ICMP_EQ:
  1567. if (isa<ConstantInt>(C2))
  1568. return ConstantExpr::getXor(C1, ConstantExpr::getNot(C2));
  1569. return ConstantExpr::getXor(ConstantExpr::getNot(C1), C2);
  1570. case ICmpInst::ICMP_NE:
  1571. return ConstantExpr::getXor(C1, C2);
  1572. default:
  1573. break;
  1574. }
  1575. }
  1576. if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) {
  1577. APInt V1 = cast<ConstantInt>(C1)->getValue();
  1578. APInt V2 = cast<ConstantInt>(C2)->getValue();
  1579. switch (pred) {
  1580. default: llvm_unreachable("Invalid ICmp Predicate");
  1581. case ICmpInst::ICMP_EQ: return ConstantInt::get(ResultTy, V1 == V2);
  1582. case ICmpInst::ICMP_NE: return ConstantInt::get(ResultTy, V1 != V2);
  1583. case ICmpInst::ICMP_SLT: return ConstantInt::get(ResultTy, V1.slt(V2));
  1584. case ICmpInst::ICMP_SGT: return ConstantInt::get(ResultTy, V1.sgt(V2));
  1585. case ICmpInst::ICMP_SLE: return ConstantInt::get(ResultTy, V1.sle(V2));
  1586. case ICmpInst::ICMP_SGE: return ConstantInt::get(ResultTy, V1.sge(V2));
  1587. case ICmpInst::ICMP_ULT: return ConstantInt::get(ResultTy, V1.ult(V2));
  1588. case ICmpInst::ICMP_UGT: return ConstantInt::get(ResultTy, V1.ugt(V2));
  1589. case ICmpInst::ICMP_ULE: return ConstantInt::get(ResultTy, V1.ule(V2));
  1590. case ICmpInst::ICMP_UGE: return ConstantInt::get(ResultTy, V1.uge(V2));
  1591. }
  1592. } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) {
  1593. APFloat C1V = cast<ConstantFP>(C1)->getValueAPF();
  1594. APFloat C2V = cast<ConstantFP>(C2)->getValueAPF();
  1595. APFloat::cmpResult R = C1V.compare(C2V);
  1596. switch (pred) {
  1597. default: llvm_unreachable("Invalid FCmp Predicate");
  1598. case FCmpInst::FCMP_FALSE: return Constant::getNullValue(ResultTy);
  1599. case FCmpInst::FCMP_TRUE: return Constant::getAllOnesValue(ResultTy);
  1600. case FCmpInst::FCMP_UNO:
  1601. return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered);
  1602. case FCmpInst::FCMP_ORD:
  1603. return ConstantInt::get(ResultTy, R!=APFloat::cmpUnordered);
  1604. case FCmpInst::FCMP_UEQ:
  1605. return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
  1606. R==APFloat::cmpEqual);
  1607. case FCmpInst::FCMP_OEQ:
  1608. return ConstantInt::get(ResultTy, R==APFloat::cmpEqual);
  1609. case FCmpInst::FCMP_UNE:
  1610. return ConstantInt::get(ResultTy, R!=APFloat::cmpEqual);
  1611. case FCmpInst::FCMP_ONE:
  1612. return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan ||
  1613. R==APFloat::cmpGreaterThan);
  1614. case FCmpInst::FCMP_ULT:
  1615. return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
  1616. R==APFloat::cmpLessThan);
  1617. case FCmpInst::FCMP_OLT:
  1618. return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan);
  1619. case FCmpInst::FCMP_UGT:
  1620. return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered ||
  1621. R==APFloat::cmpGreaterThan);
  1622. case FCmpInst::FCMP_OGT:
  1623. return ConstantInt::get(ResultTy, R==APFloat::cmpGreaterThan);
  1624. case FCmpInst::FCMP_ULE:
  1625. return ConstantInt::get(ResultTy, R!=APFloat::cmpGreaterThan);
  1626. case FCmpInst::FCMP_OLE:
  1627. return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan ||
  1628. R==APFloat::cmpEqual);
  1629. case FCmpInst::FCMP_UGE:
  1630. return ConstantInt::get(ResultTy, R!=APFloat::cmpLessThan);
  1631. case FCmpInst::FCMP_OGE:
  1632. return ConstantInt::get(ResultTy, R==APFloat::cmpGreaterThan ||
  1633. R==APFloat::cmpEqual);
  1634. }
  1635. } else if (C1->getType()->isVectorTy()) {
  1636. // If we can constant fold the comparison of each element, constant fold
  1637. // the whole vector comparison.
  1638. SmallVector<Constant*, 4> ResElts;
  1639. Type *Ty = IntegerType::get(C1->getContext(), 32);
  1640. // Compare the elements, producing an i1 result or constant expr.
  1641. for (unsigned i = 0, e = C1->getType()->getVectorNumElements(); i != e;++i){
  1642. Constant *C1E =
  1643. ConstantExpr::getExtractElement(C1, ConstantInt::get(Ty, i));
  1644. Constant *C2E =
  1645. ConstantExpr::getExtractElement(C2, ConstantInt::get(Ty, i));
  1646. ResElts.push_back(ConstantExpr::getCompare(pred, C1E, C2E));
  1647. }
  1648. return ConstantVector::get(ResElts);
  1649. }
  1650. if (C1->getType()->isFloatingPointTy() &&
  1651. // Only call evaluateFCmpRelation if we have a constant expr to avoid
  1652. // infinite recursive loop
  1653. (isa<ConstantExpr>(C1) || isa<ConstantExpr>(C2))) {
  1654. int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
  1655. switch (evaluateFCmpRelation(C1, C2)) {
  1656. default: llvm_unreachable("Unknown relation!");
  1657. case FCmpInst::FCMP_UNO:
  1658. case FCmpInst::FCMP_ORD:
  1659. case FCmpInst::FCMP_UEQ:
  1660. case FCmpInst::FCMP_UNE:
  1661. case FCmpInst::FCMP_ULT:
  1662. case FCmpInst::FCMP_UGT:
  1663. case FCmpInst::FCMP_ULE:
  1664. case FCmpInst::FCMP_UGE:
  1665. case FCmpInst::FCMP_TRUE:
  1666. case FCmpInst::FCMP_FALSE:
  1667. case FCmpInst::BAD_FCMP_PREDICATE:
  1668. break; // Couldn't determine anything about these constants.
  1669. case FCmpInst::FCMP_OEQ: // We know that C1 == C2
  1670. Result = (pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ ||
  1671. pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE ||
  1672. pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
  1673. break;
  1674. case FCmpInst::FCMP_OLT: // We know that C1 < C2
  1675. Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
  1676. pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT ||
  1677. pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE);
  1678. break;
  1679. case FCmpInst::FCMP_OGT: // We know that C1 > C2
  1680. Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE ||
  1681. pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT ||
  1682. pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE);
  1683. break;
  1684. case FCmpInst::FCMP_OLE: // We know that C1 <= C2
  1685. // We can only partially decide this relation.
  1686. if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
  1687. Result = 0;
  1688. else if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
  1689. Result = 1;
  1690. break;
  1691. case FCmpInst::FCMP_OGE: // We known that C1 >= C2
  1692. // We can only partially decide this relation.
  1693. if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT)
  1694. Result = 0;
  1695. else if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT)
  1696. Result = 1;
  1697. break;
  1698. case FCmpInst::FCMP_ONE: // We know that C1 != C2
  1699. // We can only partially decide this relation.
  1700. if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ)
  1701. Result = 0;
  1702. else if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE)
  1703. Result = 1;
  1704. break;
  1705. }
  1706. // If we evaluated the result, return it now.
  1707. if (Result != -1)
  1708. return ConstantInt::get(ResultTy, Result);
  1709. } else {
  1710. // Evaluate the relation between the two constants, per the predicate.
  1711. int Result = -1; // -1 = unknown, 0 = known false, 1 = known true.
  1712. switch (evaluateICmpRelation(C1, C2, CmpInst::isSigned(pred))) {
  1713. default: llvm_unreachable("Unknown relational!");
  1714. case ICmpInst::BAD_ICMP_PREDICATE:
  1715. break; // Couldn't determine anything about these constants.
  1716. case ICmpInst::ICMP_EQ: // We know the constants are equal!
  1717. // If we know the constants are equal, we can decide the result of this
  1718. // computation precisely.
  1719. Result = ICmpInst::isTrueWhenEqual((ICmpInst::Predicate)pred);
  1720. break;
  1721. case ICmpInst::ICMP_ULT:
  1722. switch (pred) {
  1723. case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_ULE:
  1724. Result = 1; break;
  1725. case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_UGE:
  1726. Result = 0; break;
  1727. }
  1728. break;
  1729. case ICmpInst::ICMP_SLT:
  1730. switch (pred) {
  1731. case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SLE:
  1732. Result = 1; break;
  1733. case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SGE:
  1734. Result = 0; break;
  1735. }
  1736. break;
  1737. case ICmpInst::ICMP_UGT:
  1738. switch (pred) {
  1739. case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGE:
  1740. Result = 1; break;
  1741. case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_ULE:
  1742. Result = 0; break;
  1743. }
  1744. break;
  1745. case ICmpInst::ICMP_SGT:
  1746. switch (pred) {
  1747. case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SGE:
  1748. Result = 1; break;
  1749. case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SLE:
  1750. Result = 0; break;
  1751. }
  1752. break;
  1753. case ICmpInst::ICMP_ULE:
  1754. if (pred == ICmpInst::ICMP_UGT) Result = 0;
  1755. if (pred == ICmpInst::ICMP_ULT || pred == ICmpInst::ICMP_ULE) Result = 1;
  1756. break;
  1757. case ICmpInst::ICMP_SLE:
  1758. if (pred == ICmpInst::ICMP_SGT) Result = 0;
  1759. if (pred == ICmpInst::ICMP_SLT || pred == ICmpInst::ICMP_SLE) Result = 1;
  1760. break;
  1761. case ICmpInst::ICMP_UGE:
  1762. if (pred == ICmpInst::ICMP_ULT) Result = 0;
  1763. if (pred == ICmpInst::ICMP_UGT || pred == ICmpInst::ICMP_UGE) Result = 1;
  1764. break;
  1765. case ICmpInst::ICMP_SGE:
  1766. if (pred == ICmpInst::ICMP_SLT) Result = 0;
  1767. if (pred == ICmpInst::ICMP_SGT || pred == ICmpInst::ICMP_SGE) Result = 1;
  1768. break;
  1769. case ICmpInst::ICMP_NE:
  1770. if (pred == ICmpInst::ICMP_EQ) Result = 0;
  1771. if (pred == ICmpInst::ICMP_NE) Result = 1;
  1772. break;
  1773. }
  1774. // If we evaluated the result, return it now.
  1775. if (Result != -1)
  1776. return ConstantInt::get(ResultTy, Result);
  1777. // If the right hand side is a bitcast, try using its inverse to simplify
  1778. // it by moving it to the left hand side. We can't do this if it would turn
  1779. // a vector compare into a scalar compare or visa versa.
  1780. if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(C2)) {
  1781. Constant *CE2Op0 = CE2->getOperand(0);
  1782. if (CE2->getOpcode() == Instruction::BitCast &&
  1783. CE2->getType()->isVectorTy() == CE2Op0->getType()->isVectorTy()) {
  1784. Constant *Inverse = ConstantExpr::getBitCast(C1, CE2Op0->getType());
  1785. return ConstantExpr::getICmp(pred, Inverse, CE2Op0);
  1786. }
  1787. }
  1788. // If the left hand side is an extension, try eliminating it.
  1789. if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) {
  1790. if ((CE1->getOpcode() == Instruction::SExt && ICmpInst::isSigned(pred)) ||
  1791. (CE1->getOpcode() == Instruction::ZExt && !ICmpInst::isSigned(pred))){
  1792. Constant *CE1Op0 = CE1->getOperand(0);
  1793. Constant *CE1Inverse = ConstantExpr::getTrunc(CE1, CE1Op0->getType());
  1794. if (CE1Inverse == CE1Op0) {
  1795. // Check whether we can safely truncate the right hand side.
  1796. Constant *C2Inverse = ConstantExpr::getTrunc(C2, CE1Op0->getType());
  1797. if (ConstantExpr::getCast(CE1->getOpcode(), C2Inverse,
  1798. C2->getType()) == C2)
  1799. return ConstantExpr::getICmp(pred, CE1Inverse, C2Inverse);
  1800. }
  1801. }
  1802. }
  1803. if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) ||
  1804. (C1->isNullValue() && !C2->isNullValue())) {
  1805. // If C2 is a constant expr and C1 isn't, flip them around and fold the
  1806. // other way if possible.
  1807. // Also, if C1 is null and C2 isn't, flip them around.
  1808. pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred);
  1809. return ConstantExpr::getICmp(pred, C2, C1);
  1810. }
  1811. }
  1812. return nullptr;
  1813. }
  1814. /// isInBoundsIndices - Test whether the given sequence of *normalized* indices
  1815. /// is "inbounds".
  1816. template<typename IndexTy>
  1817. static bool isInBoundsIndices(ArrayRef<IndexTy> Idxs) {
  1818. // No indices means nothing that could be out of bounds.
  1819. if (Idxs.empty()) return true;
  1820. // If the first index is zero, it's in bounds.
  1821. if (cast<Constant>(Idxs[0])->isNullValue()) return true;
  1822. // If the first index is one and all the rest are zero, it's in bounds,
  1823. // by the one-past-the-end rule.
  1824. if (!cast<ConstantInt>(Idxs[0])->isOne())
  1825. return false;
  1826. for (unsigned i = 1, e = Idxs.size(); i != e; ++i)
  1827. if (!cast<Constant>(Idxs[i])->isNullValue())
  1828. return false;
  1829. return true;
  1830. }
  1831. /// \brief Test whether a given ConstantInt is in-range for a SequentialType.
  1832. static bool isIndexInRangeOfSequentialType(const SequentialType *STy,
  1833. const ConstantInt *CI) {
  1834. if (const PointerType *PTy = dyn_cast<PointerType>(STy))
  1835. // Only handle pointers to sized types, not pointers to functions.
  1836. return PTy->getElementType()->isSized();
  1837. uint64_t NumElements = 0;
  1838. // Determine the number of elements in our sequential type.
  1839. if (const ArrayType *ATy = dyn_cast<ArrayType>(STy))
  1840. NumElements = ATy->getNumElements();
  1841. else if (const VectorType *VTy = dyn_cast<VectorType>(STy))
  1842. NumElements = VTy->getNumElements();
  1843. assert((isa<ArrayType>(STy) || NumElements > 0) &&
  1844. "didn't expect non-array type to have zero elements!");
  1845. // We cannot bounds check the index if it doesn't fit in an int64_t.
  1846. if (CI->getValue().getActiveBits() > 64)
  1847. return false;
  1848. // A negative index or an index past the end of our sequential type is
  1849. // considered out-of-range.
  1850. int64_t IndexVal = CI->getSExtValue();
  1851. if (IndexVal < 0 || (NumElements > 0 && (uint64_t)IndexVal >= NumElements))
  1852. return false;
  1853. // Otherwise, it is in-range.
  1854. return true;
  1855. }
  1856. template<typename IndexTy>
  1857. static Constant *ConstantFoldGetElementPtrImpl(Type *PointeeTy, Constant *C,
  1858. bool inBounds,
  1859. ArrayRef<IndexTy> Idxs) {
  1860. if (Idxs.empty()) return C;
  1861. Constant *Idx0 = cast<Constant>(Idxs[0]);
  1862. if ((Idxs.size() == 1 && Idx0->isNullValue()))
  1863. return C;
  1864. if (isa<UndefValue>(C)) {
  1865. PointerType *Ptr = cast<PointerType>(C->getType());
  1866. Type *Ty = GetElementPtrInst::getIndexedType(
  1867. cast<PointerType>(Ptr->getScalarType())->getElementType(), Idxs);
  1868. assert(Ty && "Invalid indices for GEP!");
  1869. return UndefValue::get(PointerType::get(Ty, Ptr->getAddressSpace()));
  1870. }
  1871. if (C->isNullValue()) {
  1872. bool isNull = true;
  1873. for (unsigned i = 0, e = Idxs.size(); i != e; ++i)
  1874. if (!cast<Constant>(Idxs[i])->isNullValue()) {
  1875. isNull = false;
  1876. break;
  1877. }
  1878. if (isNull) {
  1879. PointerType *Ptr = cast<PointerType>(C->getType());
  1880. Type *Ty = GetElementPtrInst::getIndexedType(
  1881. cast<PointerType>(Ptr->getScalarType())->getElementType(), Idxs);
  1882. assert(Ty && "Invalid indices for GEP!");
  1883. return ConstantPointerNull::get(PointerType::get(Ty,
  1884. Ptr->getAddressSpace()));
  1885. }
  1886. }
  1887. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
  1888. // Combine Indices - If the source pointer to this getelementptr instruction
  1889. // is a getelementptr instruction, combine the indices of the two
  1890. // getelementptr instructions into a single instruction.
  1891. //
  1892. if (CE->getOpcode() == Instruction::GetElementPtr) {
  1893. Type *LastTy = nullptr;
  1894. for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE);
  1895. I != E; ++I)
  1896. LastTy = *I;
  1897. // We cannot combine indices if doing so would take us outside of an
  1898. // array or vector. Doing otherwise could trick us if we evaluated such a
  1899. // GEP as part of a load.
  1900. //
  1901. // e.g. Consider if the original GEP was:
  1902. // i8* getelementptr ({ [2 x i8], i32, i8, [3 x i8] }* @main.c,
  1903. // i32 0, i32 0, i64 0)
  1904. //
  1905. // If we then tried to offset it by '8' to get to the third element,
  1906. // an i8, we should *not* get:
  1907. // i8* getelementptr ({ [2 x i8], i32, i8, [3 x i8] }* @main.c,
  1908. // i32 0, i32 0, i64 8)
  1909. //
  1910. // This GEP tries to index array element '8 which runs out-of-bounds.
  1911. // Subsequent evaluation would get confused and produce erroneous results.
  1912. //
  1913. // The following prohibits such a GEP from being formed by checking to see
  1914. // if the index is in-range with respect to an array or vector.
  1915. bool PerformFold = false;
  1916. if (Idx0->isNullValue())
  1917. PerformFold = true;
  1918. else if (SequentialType *STy = dyn_cast_or_null<SequentialType>(LastTy))
  1919. if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx0))
  1920. PerformFold = isIndexInRangeOfSequentialType(STy, CI);
  1921. if (PerformFold) {
  1922. SmallVector<Value*, 16> NewIndices;
  1923. NewIndices.reserve(Idxs.size() + CE->getNumOperands());
  1924. NewIndices.append(CE->op_begin() + 1, CE->op_end() - 1);
  1925. // Add the last index of the source with the first index of the new GEP.
  1926. // Make sure to handle the case when they are actually different types.
  1927. Constant *Combined = CE->getOperand(CE->getNumOperands()-1);
  1928. // Otherwise it must be an array.
  1929. if (!Idx0->isNullValue()) {
  1930. Type *IdxTy = Combined->getType();
  1931. if (IdxTy != Idx0->getType()) {
  1932. unsigned CommonExtendedWidth =
  1933. std::max(IdxTy->getIntegerBitWidth(),
  1934. Idx0->getType()->getIntegerBitWidth());
  1935. CommonExtendedWidth = std::max(CommonExtendedWidth, 64U);
  1936. Type *CommonTy =
  1937. Type::getIntNTy(IdxTy->getContext(), CommonExtendedWidth);
  1938. Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, CommonTy);
  1939. Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined, CommonTy);
  1940. Combined = ConstantExpr::get(Instruction::Add, C1, C2);
  1941. } else {
  1942. Combined =
  1943. ConstantExpr::get(Instruction::Add, Idx0, Combined);
  1944. }
  1945. }
  1946. NewIndices.push_back(Combined);
  1947. NewIndices.append(Idxs.begin() + 1, Idxs.end());
  1948. return ConstantExpr::getGetElementPtr(
  1949. cast<GEPOperator>(CE)->getSourceElementType(), CE->getOperand(0),
  1950. NewIndices, inBounds && cast<GEPOperator>(CE)->isInBounds());
  1951. }
  1952. }
  1953. // Attempt to fold casts to the same type away. For example, folding:
  1954. //
  1955. // i32* getelementptr ([2 x i32]* bitcast ([3 x i32]* %X to [2 x i32]*),
  1956. // i64 0, i64 0)
  1957. // into:
  1958. //
  1959. // i32* getelementptr ([3 x i32]* %X, i64 0, i64 0)
  1960. //
  1961. // Don't fold if the cast is changing address spaces.
  1962. if (CE->isCast() && Idxs.size() > 1 && Idx0->isNullValue()) {
  1963. PointerType *SrcPtrTy =
  1964. dyn_cast<PointerType>(CE->getOperand(0)->getType());
  1965. PointerType *DstPtrTy = dyn_cast<PointerType>(CE->getType());
  1966. if (SrcPtrTy && DstPtrTy) {
  1967. ArrayType *SrcArrayTy =
  1968. dyn_cast<ArrayType>(SrcPtrTy->getElementType());
  1969. ArrayType *DstArrayTy =
  1970. dyn_cast<ArrayType>(DstPtrTy->getElementType());
  1971. if (SrcArrayTy && DstArrayTy
  1972. && SrcArrayTy->getElementType() == DstArrayTy->getElementType()
  1973. && SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
  1974. return ConstantExpr::getGetElementPtr(
  1975. SrcArrayTy, (Constant *)CE->getOperand(0), Idxs, inBounds);
  1976. }
  1977. }
  1978. }
  1979. // Check to see if any array indices are not within the corresponding
  1980. // notional array or vector bounds. If so, try to determine if they can be
  1981. // factored out into preceding dimensions.
  1982. SmallVector<Constant *, 8> NewIdxs;
  1983. Type *Ty = PointeeTy;
  1984. Type *Prev = C->getType();
  1985. bool Unknown = !isa<ConstantInt>(Idxs[0]);
  1986. for (unsigned i = 1, e = Idxs.size(); i != e;
  1987. Prev = Ty, Ty = cast<CompositeType>(Ty)->getTypeAtIndex(Idxs[i]), ++i) {
  1988. if (ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) {
  1989. if (isa<ArrayType>(Ty) || isa<VectorType>(Ty))
  1990. if (CI->getSExtValue() > 0 &&
  1991. !isIndexInRangeOfSequentialType(cast<SequentialType>(Ty), CI)) {
  1992. if (isa<SequentialType>(Prev)) {
  1993. // It's out of range, but we can factor it into the prior
  1994. // dimension.
  1995. NewIdxs.resize(Idxs.size());
  1996. uint64_t NumElements = 0;
  1997. if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty))
  1998. NumElements = ATy->getNumElements();
  1999. else
  2000. NumElements = cast<VectorType>(Ty)->getNumElements();
  2001. ConstantInt *Factor = ConstantInt::get(CI->getType(), NumElements);
  2002. NewIdxs[i] = ConstantExpr::getSRem(CI, Factor);
  2003. Constant *PrevIdx = cast<Constant>(Idxs[i-1]);
  2004. Constant *Div = ConstantExpr::getSDiv(CI, Factor);
  2005. unsigned CommonExtendedWidth =
  2006. std::max(PrevIdx->getType()->getIntegerBitWidth(),
  2007. Div->getType()->getIntegerBitWidth());
  2008. CommonExtendedWidth = std::max(CommonExtendedWidth, 64U);
  2009. // Before adding, extend both operands to i64 to avoid
  2010. // overflow trouble.
  2011. if (!PrevIdx->getType()->isIntegerTy(CommonExtendedWidth))
  2012. PrevIdx = ConstantExpr::getSExt(
  2013. PrevIdx,
  2014. Type::getIntNTy(Div->getContext(), CommonExtendedWidth));
  2015. if (!Div->getType()->isIntegerTy(CommonExtendedWidth))
  2016. Div = ConstantExpr::getSExt(
  2017. Div, Type::getIntNTy(Div->getContext(), CommonExtendedWidth));
  2018. NewIdxs[i-1] = ConstantExpr::getAdd(PrevIdx, Div);
  2019. } else {
  2020. // It's out of range, but the prior dimension is a struct
  2021. // so we can't do anything about it.
  2022. Unknown = true;
  2023. }
  2024. }
  2025. } else {
  2026. // We don't know if it's in range or not.
  2027. Unknown = true;
  2028. }
  2029. }
  2030. // If we did any factoring, start over with the adjusted indices.
  2031. if (!NewIdxs.empty()) {
  2032. for (unsigned i = 0, e = Idxs.size(); i != e; ++i)
  2033. if (!NewIdxs[i]) NewIdxs[i] = cast<Constant>(Idxs[i]);
  2034. return ConstantExpr::getGetElementPtr(PointeeTy, C, NewIdxs, inBounds);
  2035. }
  2036. // If all indices are known integers and normalized, we can do a simple
  2037. // check for the "inbounds" property.
  2038. if (!Unknown && !inBounds)
  2039. if (auto *GV = dyn_cast<GlobalVariable>(C))
  2040. if (!GV->hasExternalWeakLinkage() && isInBoundsIndices(Idxs))
  2041. return ConstantExpr::getInBoundsGetElementPtr(PointeeTy, C, Idxs);
  2042. return nullptr;
  2043. }
  2044. Constant *llvm::ConstantFoldGetElementPtr(Constant *C,
  2045. bool inBounds,
  2046. ArrayRef<Constant *> Idxs) {
  2047. return ConstantFoldGetElementPtrImpl(
  2048. cast<PointerType>(C->getType()->getScalarType())->getElementType(), C,
  2049. inBounds, Idxs);
  2050. }
  2051. Constant *llvm::ConstantFoldGetElementPtr(Constant *C,
  2052. bool inBounds,
  2053. ArrayRef<Value *> Idxs) {
  2054. return ConstantFoldGetElementPtrImpl(
  2055. cast<PointerType>(C->getType()->getScalarType())->getElementType(), C,
  2056. inBounds, Idxs);
  2057. }
  2058. Constant *llvm::ConstantFoldGetElementPtr(Type *Ty, Constant *C,
  2059. bool inBounds,
  2060. ArrayRef<Constant *> Idxs) {
  2061. return ConstantFoldGetElementPtrImpl(Ty, C, inBounds, Idxs);
  2062. }
  2063. Constant *llvm::ConstantFoldGetElementPtr(Type *Ty, Constant *C,
  2064. bool inBounds,
  2065. ArrayRef<Value *> Idxs) {
  2066. return ConstantFoldGetElementPtrImpl(Ty, C, inBounds, Idxs);
  2067. }