19472c373a3378982ab4ef1a2caafabd4acf58ba2Reid Spencer//===- ConstantFold.cpp - LLVM constant folder ----------------------------===// 2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// 3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// The LLVM Compiler Infrastructure 4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell// 54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source 64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details. 7fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// 8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===// 9009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner// 10cbfd40654177d6c2b7fcb94272c3a8064d5b8297Chris Lattner// This file implements folding of constants for LLVM. This implements the 119472c373a3378982ab4ef1a2caafabd4acf58ba2Reid Spencer// (internal) ConstantFold.h interface, which is used by the 12cbfd40654177d6c2b7fcb94272c3a8064d5b8297Chris Lattner// ConstantExpr::get* methods to automatically fold constants when possible. 13009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner// 14eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner// The current constant folding implementation is implemented in two pieces: the 153574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow// pieces that don't need DataLayout, and the pieces that do. This is to avoid 16c2c50cdcdc19a1bca993c06d13d8cdca87083ce4Chandler Carruth// a dependence in IR on Target. 17eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner// 18009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner//===----------------------------------------------------------------------===// 19009505452b713ed2e3a8e99c5545a6e721c65495Chris Lattner 2092f6feaf792469130eec7196854ff01215955964Chris Lattner#include "ConstantFold.h" 21d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/SmallVector.h" 220b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Constants.h" 230b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DerivedTypes.h" 240b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Function.h" 250b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/GlobalAlias.h" 260b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/GlobalVariable.h" 270b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Instructions.h" 280b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Operator.h" 290eff5ad3db4bc85e84c85a7fbdc6b26653ff223dChris Lattner#include "llvm/Support/Compiler.h" 30c25e7581b9b8088910da31702d4ca21c4734c6d7Torok Edwin#include "llvm/Support/ErrorHandling.h" 31b9da9c137d3062dd2277937eaafa502fba2ffe3fChris Lattner#include "llvm/Support/GetElementPtrTypeIterator.h" 320eff5ad3db4bc85e84c85a7fbdc6b26653ff223dChris Lattner#include "llvm/Support/ManagedStatic.h" 334460f4058fb33e468358f85c7f0925fc0e69e16cChris Lattner#include "llvm/Support/MathExtras.h" 34a97e8db835cd6655eb537ace67dd46be7e4585b5Jeff Cohen#include <limits> 350dc396909e01c5fb27e2a5a192131a5d79c3f0e9Chris Lattnerusing namespace llvm; 3614712a6abf2587666e8171cbb6ebe6ffab3ea514Chris Lattner 37eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner//===----------------------------------------------------------------------===// 38eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner// ConstantFold*Instruction Implementations 39eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner//===----------------------------------------------------------------------===// 40eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner 41d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner/// BitCastConstantVector - Convert the specified vector Constant node to the 42ac9dcb94dde5f166ee29372385c0e3b695227ab4Reid Spencer/// specified vector type. At this point, we know that the elements of the 4307a96765daedf180a7102d39fe56c499878312b7Dan Gohman/// input vector constant are all simple integer or FP values. 44d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattnerstatic Constant *BitCastConstantVector(Constant *CV, VectorType *DstTy) { 45d2f27ead2d71afeee869cad8ae8a1c1dce7229cbNadav Rotem 46093399cbf3bcdb31d04b3bf5c5691fc88c25da48Nadav Rotem if (CV->isAllOnesValue()) return Constant::getAllOnesValue(DstTy); 47d2f27ead2d71afeee869cad8ae8a1c1dce7229cbNadav Rotem if (CV->isNullValue()) return Constant::getNullValue(DstTy); 48d2f27ead2d71afeee869cad8ae8a1c1dce7229cbNadav Rotem 499eaa835e887a0a35161adf357af0e3d221e095d7Chris Lattner // If this cast changes element count then we can't handle it here: 509eaa835e887a0a35161adf357af0e3d221e095d7Chris Lattner // doing so requires endianness information. This should be handled by 519eaa835e887a0a35161adf357af0e3d221e095d7Chris Lattner // Analysis/ConstantFolding.cpp 529eaa835e887a0a35161adf357af0e3d221e095d7Chris Lattner unsigned NumElts = DstTy->getNumElements(); 53d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner if (NumElts != CV->getType()->getVectorNumElements()) 549eaa835e887a0a35161adf357af0e3d221e095d7Chris Lattner return 0; 55d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner 56d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner Type *DstEltTy = DstTy->getElementType(); 57f8a87e8343c5f829be9a75d8160e1bb969b84119Dan Gohman 58d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner SmallVector<Constant*, 16> Result; 5903e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman Type *Ty = IntegerType::get(CV->getContext(), 32); 609eaa835e887a0a35161adf357af0e3d221e095d7Chris Lattner for (unsigned i = 0; i != NumElts; ++i) { 6103e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman Constant *C = 6203e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman ConstantExpr::getExtractElement(CV, ConstantInt::get(Ty, i)); 63d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner C = ConstantExpr::getBitCast(C, DstEltTy); 64d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner Result.push_back(C); 654460f4058fb33e468358f85c7f0925fc0e69e16cChris Lattner } 669eaa835e887a0a35161adf357af0e3d221e095d7Chris Lattner 67af7ec975870f92245f1f1484ac80a1e2db6a0afaOwen Anderson return ConstantVector::get(Result); 684460f4058fb33e468358f85c7f0925fc0e69e16cChris Lattner} 694460f4058fb33e468358f85c7f0925fc0e69e16cChris Lattner 703da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer/// This function determines which opcode to use to fold two constant cast 713da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer/// expressions together. It uses CastInst::isEliminableCastPair to determine 723da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer/// the opcode. Consequently its just a wrapper around that function. 73daa10a46b2e94e95b3f479cc24ecbbd33fe94c87Reid Spencer/// @brief Determine if it is valid to fold a cast of a cast 743da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencerstatic unsigned 753da59db637a887474c1b1346c1f3ccf53b6c4663Reid SpencerfoldConstantCastPair( 763da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer unsigned opc, ///< opcode of the second cast constant expression 7733c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky ConstantExpr *Op, ///< the first cast constant expression 783181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault Type *DstTy ///< destination type of the first cast 793da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer) { 803da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer assert(Op && Op->isCast() && "Can't fold cast of cast without a cast!"); 813da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer assert(DstTy && DstTy->isFirstClassType() && "Invalid cast destination type"); 823da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer assert(CastInst::isCast(opc) && "Invalid cast opcode"); 83f8a87e8343c5f829be9a75d8160e1bb969b84119Dan Gohman 843da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer // The the types and opcodes for the two Cast constant expressions 85db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *SrcTy = Op->getOperand(0)->getType(); 86db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *MidTy = Op->getType(); 873da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer Instruction::CastOps firstOp = Instruction::CastOps(Op->getOpcode()); 883da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer Instruction::CastOps secondOp = Instruction::CastOps(opc); 893da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer 903181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault // Assume that pointers are never more than 64 bits wide, and only use this 913181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault // for the middle type. Otherwise we could end up folding away illegal 923181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault // bitcasts between address spaces with different sizes. 93446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands IntegerType *FakeIntPtrTy = Type::getInt64Ty(DstTy->getContext()); 94446cf94cdbbc1f8e22452fc46664ac73d810c6a2Duncan Sands 953da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer // Let CastInst::isEliminableCastPair do the heavy lifting. 963da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer return CastInst::isEliminableCastPair(firstOp, secondOp, SrcTy, MidTy, DstTy, 973181f5900ff5d9800c38284c7d3427cb6e306c9aMatt Arsenault 0, FakeIntPtrTy, 0); 983da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer} 994460f4058fb33e468358f85c7f0925fc0e69e16cChris Lattner 100db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattnerstatic Constant *FoldBitCast(Constant *V, Type *DestTy) { 101db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *SrcTy = V->getType(); 1022b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner if (SrcTy == DestTy) 1032b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner return V; // no-op cast 104f8a87e8343c5f829be9a75d8160e1bb969b84119Dan Gohman 1052b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner // Check to see if we are casting a pointer to an aggregate to a pointer to 1062b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner // the first element. If so, return the appropriate GEP instruction. 107db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner if (PointerType *PTy = dyn_cast<PointerType>(V->getType())) 108db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner if (PointerType *DPTy = dyn_cast<PointerType>(DestTy)) 1091876abe63edf0380aee3d0f3ad9cace3f7e6a042Duncan Sands if (PTy->getAddressSpace() == DPTy->getAddressSpace() 1101876abe63edf0380aee3d0f3ad9cace3f7e6a042Duncan Sands && DPTy->getElementType()->isSized()) { 11183ad90a779f8ce46d6923bc6f63aa15b83968efbNate Begeman SmallVector<Value*, 8> IdxList; 112b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner Value *Zero = 113b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner Constant::getNullValue(Type::getInt32Ty(DPTy->getContext())); 114c752064894630565bdef503f6001e849a1fcfc3fDan Gohman IdxList.push_back(Zero); 115db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *ElTy = PTy->getElementType(); 11683ad90a779f8ce46d6923bc6f63aa15b83968efbNate Begeman while (ElTy != DPTy->getElementType()) { 117db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner if (StructType *STy = dyn_cast<StructType>(ElTy)) { 11883ad90a779f8ce46d6923bc6f63aa15b83968efbNate Begeman if (STy->getNumElements() == 0) break; 11983ad90a779f8ce46d6923bc6f63aa15b83968efbNate Begeman ElTy = STy->getElementType(0); 120c752064894630565bdef503f6001e849a1fcfc3fDan Gohman IdxList.push_back(Zero); 121db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner } else if (SequentialType *STy = 12283ad90a779f8ce46d6923bc6f63aa15b83968efbNate Begeman dyn_cast<SequentialType>(ElTy)) { 1231df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands if (ElTy->isPointerTy()) break; // Can't index into pointers! 12483ad90a779f8ce46d6923bc6f63aa15b83968efbNate Begeman ElTy = STy->getElementType(); 125c752064894630565bdef503f6001e849a1fcfc3fDan Gohman IdxList.push_back(Zero); 12683ad90a779f8ce46d6923bc6f63aa15b83968efbNate Begeman } else { 12783ad90a779f8ce46d6923bc6f63aa15b83968efbNate Begeman break; 12883ad90a779f8ce46d6923bc6f63aa15b83968efbNate Begeman } 1292b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner } 130f8a87e8343c5f829be9a75d8160e1bb969b84119Dan Gohman 13183ad90a779f8ce46d6923bc6f63aa15b83968efbNate Begeman if (ElTy == DPTy->getElementType()) 1326e7ad958683f34bf6c014c88fef723e5a2d741beDan Gohman // This GEP is inbounds because all indices are zero. 133dab3d29605a5c83db41b28176273ef55961120c1Jay Foad return ConstantExpr::getInBoundsGetElementPtr(V, IdxList); 1342b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner } 135f8a87e8343c5f829be9a75d8160e1bb969b84119Dan Gohman 1362b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner // Handle casts from one vector constant to another. We know that the src 1372b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner // and dest type have the same size (otherwise its an illegal cast). 138db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner if (VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) { 139db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner if (VectorType *SrcTy = dyn_cast<VectorType>(V->getType())) { 1402b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner assert(DestPTy->getBitWidth() == SrcTy->getBitWidth() && 1412b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner "Not cast between same sized vectors!"); 142b6dc9352b0d8361609c310699ce58a517fdc2264Devang Patel SrcTy = NULL; 1432b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner // First, check for null. Undef is already handled. 1442b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner if (isa<ConstantAggregateZero>(V)) 145a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson return Constant::getNullValue(DestTy); 146f8a87e8343c5f829be9a75d8160e1bb969b84119Dan Gohman 147d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner // Handle ConstantVector and ConstantAggregateVector. 148d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner return BitCastConstantVector(V, DestPTy); 1492b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner } 150868510328fc0b25ca97f2fba466a77c2c4e29c45Chris Lattner 151868510328fc0b25ca97f2fba466a77c2c4e29c45Chris Lattner // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts 152868510328fc0b25ca97f2fba466a77c2c4e29c45Chris Lattner // This allows for other simplifications (although some of them 153868510328fc0b25ca97f2fba466a77c2c4e29c45Chris Lattner // can only be handled by Analysis/ConstantFolding.cpp). 154868510328fc0b25ca97f2fba466a77c2c4e29c45Chris Lattner if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) 1552ca5c8644e6c35b3a7910a576ed89cddb7b82c3bChris Lattner return ConstantExpr::getBitCast(ConstantVector::get(V), DestPTy); 1562b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner } 157f8a87e8343c5f829be9a75d8160e1bb969b84119Dan Gohman 1582b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner // Finally, implement bitcast folding now. The code below doesn't handle 1592b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner // bitcast right. 1602b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner if (isa<ConstantPointerNull>(V)) // ptr->ptr cast. 1619e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson return ConstantPointerNull::get(cast<PointerType>(DestTy)); 162f8a87e8343c5f829be9a75d8160e1bb969b84119Dan Gohman 1632b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner // Handle integral constant input. 16433c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 165b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands if (DestTy->isIntegerTy()) 1662b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner // Integral -> Integral. This is a no-op because the bit widths must 1672b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner // be the same. Consequently, we just fold to V. 1682b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner return V; 169a06aef6ec55552640e45ffada96e43cae96cfc01Duncan Sands 170b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands if (DestTy->isFloatingPointTy()) 171b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantFP::get(DestTy->getContext(), 1720a29cb045444c13160e90fe7942a9d7c720185edTim Northover APFloat(DestTy->getFltSemantics(), 1730a29cb045444c13160e90fe7942a9d7c720185edTim Northover CI->getValue())); 174a06aef6ec55552640e45ffada96e43cae96cfc01Duncan Sands 1752b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner // Otherwise, can't fold this (vector?) 1762b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner return 0; 1772b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner } 17809750273a4cd49630b64ec03b2e2f207a4fe8308Duncan Sands 179b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner // Handle ConstantFP input: FP -> Integral. 18033c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (ConstantFP *FP = dyn_cast<ConstantFP>(V)) 181b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(FP->getContext(), 182b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner FP->getValueAPF().bitcastToAPInt()); 18309750273a4cd49630b64ec03b2e2f207a4fe8308Duncan Sands 1842b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner return 0; 1852b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner} 1862b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner 1872b0f806c88f022094112ccc3aeb31c7d55f50874Chris Lattner 18859c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner/// ExtractConstantBytes - V is an integer constant which only has a subset of 18959c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner/// its bytes used. The bytes used are indicated by ByteStart (which is the 19059c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner/// first byte used, counting from the least significant byte) and ByteSize, 19159c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner/// which is the number of bytes used. 19259c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner/// 19359c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner/// This function analyzes the specified constant to see if the specified byte 19459c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner/// range can be returned as a simplified constant. If so, the constant is 19559c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner/// returned, otherwise null is returned. 19659c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner/// 19759c4eba4169e40b36719c521c25d29115ff6b206Chris Lattnerstatic Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart, 19859c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner unsigned ByteSize) { 1991df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands assert(C->getType()->isIntegerTy() && 20059c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner (cast<IntegerType>(C->getType())->getBitWidth() & 7) == 0 && 20159c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner "Non-byte sized integer input"); 20259c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner unsigned CSize = cast<IntegerType>(C->getType())->getBitWidth()/8; 20359c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner assert(ByteSize && "Must be accessing some piece"); 20459c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner assert(ByteStart+ByteSize <= CSize && "Extracting invalid piece from input"); 20559c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner assert(ByteSize != CSize && "Should not extract everything"); 20659c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner 20759c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner // Constant Integers are simple. 20859c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) { 20959c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner APInt V = CI->getValue(); 21059c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if (ByteStart) 21159c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner V = V.lshr(ByteStart*8); 21240f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad V = V.trunc(ByteSize*8); 21359c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner return ConstantInt::get(CI->getContext(), V); 21459c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner } 21559c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner 21659c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner // In the input is a constant expr, we might be able to recursively simplify. 21759c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner // If not, we definitely can't do anything. 21859c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner ConstantExpr *CE = dyn_cast<ConstantExpr>(C); 21959c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if (CE == 0) return 0; 22059c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner 22159c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner switch (CE->getOpcode()) { 22259c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner default: return 0; 22359c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner case Instruction::Or: { 2240eeb913aa17a68b1f2963b02ca1d68f09dba0b78Chris Lattner Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize); 22559c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if (RHS == 0) 22659c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner return 0; 22759c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner 22859c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner // X | -1 -> -1. 22959c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if (ConstantInt *RHSC = dyn_cast<ConstantInt>(RHS)) 23059c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if (RHSC->isAllOnesValue()) 23159c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner return RHSC; 23259c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner 2330eeb913aa17a68b1f2963b02ca1d68f09dba0b78Chris Lattner Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize); 23459c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if (LHS == 0) 23559c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner return 0; 23659c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner return ConstantExpr::getOr(LHS, RHS); 23759c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner } 23859c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner case Instruction::And: { 2390eeb913aa17a68b1f2963b02ca1d68f09dba0b78Chris Lattner Constant *RHS = ExtractConstantBytes(CE->getOperand(1), ByteStart,ByteSize); 24059c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if (RHS == 0) 24159c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner return 0; 24259c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner 24359c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner // X & 0 -> 0. 24459c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if (RHS->isNullValue()) 24559c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner return RHS; 24659c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner 2470eeb913aa17a68b1f2963b02ca1d68f09dba0b78Chris Lattner Constant *LHS = ExtractConstantBytes(CE->getOperand(0), ByteStart,ByteSize); 24859c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if (LHS == 0) 24959c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner return 0; 25059c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner return ConstantExpr::getAnd(LHS, RHS); 25159c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner } 25259c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner case Instruction::LShr: { 25359c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1)); 25459c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if (Amt == 0) 25559c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner return 0; 25659c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner unsigned ShAmt = Amt->getZExtValue(); 25759c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner // Cannot analyze non-byte shifts. 25859c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if ((ShAmt & 7) != 0) 25959c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner return 0; 26059c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner ShAmt >>= 3; 26159c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner 26259c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner // If the extract is known to be all zeros, return zero. 26359c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if (ByteStart >= CSize-ShAmt) 26459c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner return Constant::getNullValue(IntegerType::get(CE->getContext(), 26559c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner ByteSize*8)); 26659c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner // If the extract is known to be fully in the input, extract it. 26759c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if (ByteStart+ByteSize+ShAmt <= CSize) 2680eeb913aa17a68b1f2963b02ca1d68f09dba0b78Chris Lattner return ExtractConstantBytes(CE->getOperand(0), ByteStart+ShAmt, ByteSize); 26959c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner 27059c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner // TODO: Handle the 'partially zero' case. 27159c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner return 0; 27259c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner } 27359c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner 27459c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner case Instruction::Shl: { 27559c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner ConstantInt *Amt = dyn_cast<ConstantInt>(CE->getOperand(1)); 27659c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if (Amt == 0) 27759c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner return 0; 27859c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner unsigned ShAmt = Amt->getZExtValue(); 27959c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner // Cannot analyze non-byte shifts. 28059c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if ((ShAmt & 7) != 0) 28159c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner return 0; 28259c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner ShAmt >>= 3; 28359c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner 28459c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner // If the extract is known to be all zeros, return zero. 28559c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if (ByteStart+ByteSize <= ShAmt) 28659c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner return Constant::getNullValue(IntegerType::get(CE->getContext(), 28759c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner ByteSize*8)); 28859c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner // If the extract is known to be fully in the input, extract it. 28959c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if (ByteStart >= ShAmt) 2900eeb913aa17a68b1f2963b02ca1d68f09dba0b78Chris Lattner return ExtractConstantBytes(CE->getOperand(0), ByteStart-ShAmt, ByteSize); 29159c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner 29259c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner // TODO: Handle the 'partially zero' case. 29359c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner return 0; 29459c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner } 29559c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner 29659c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner case Instruction::ZExt: { 29759c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner unsigned SrcBitSize = 2980eeb913aa17a68b1f2963b02ca1d68f09dba0b78Chris Lattner cast<IntegerType>(CE->getOperand(0)->getType())->getBitWidth(); 29959c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner 30059c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner // If extracting something that is completely zero, return 0. 30159c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if (ByteStart*8 >= SrcBitSize) 30259c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner return Constant::getNullValue(IntegerType::get(CE->getContext(), 30359c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner ByteSize*8)); 30459c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner 30559c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner // If exactly extracting the input, return it. 30659c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if (ByteStart == 0 && ByteSize*8 == SrcBitSize) 3070eeb913aa17a68b1f2963b02ca1d68f09dba0b78Chris Lattner return CE->getOperand(0); 30859c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner 30959c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner // If extracting something completely in the input, if if the input is a 31059c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner // multiple of 8 bits, recurse. 31159c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if ((SrcBitSize&7) == 0 && (ByteStart+ByteSize)*8 <= SrcBitSize) 3120eeb913aa17a68b1f2963b02ca1d68f09dba0b78Chris Lattner return ExtractConstantBytes(CE->getOperand(0), ByteStart, ByteSize); 31359c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner 31459c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner // Otherwise, if extracting a subset of the input, which is not multiple of 31559c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner // 8 bits, do a shift and trunc to get the bits. 31659c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if ((ByteStart+ByteSize)*8 < SrcBitSize) { 31759c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner assert((SrcBitSize&7) && "Shouldn't get byte sized case here"); 3180eeb913aa17a68b1f2963b02ca1d68f09dba0b78Chris Lattner Constant *Res = CE->getOperand(0); 31959c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if (ByteStart) 32059c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner Res = ConstantExpr::getLShr(Res, 32159c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner ConstantInt::get(Res->getType(), ByteStart*8)); 32259c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner return ConstantExpr::getTrunc(Res, IntegerType::get(C->getContext(), 32359c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner ByteSize*8)); 32459c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner } 32559c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner 32659c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner // TODO: Handle the 'partially zero' case. 32759c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner return 0; 32859c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner } 32959c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner } 33059c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner} 33159c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner 3324f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman/// getFoldedSizeOf - Return a ConstantExpr with type DestTy for sizeof 3334f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman/// on Ty, with any known factors factored out. If Folded is false, 3344f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman/// return null if no factoring was possible, to avoid endlessly 3354f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman/// bouncing an unfoldable expression back into the top-level folder. 3364f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman/// 337db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattnerstatic Constant *getFoldedSizeOf(Type *Ty, Type *DestTy, 3384f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman bool Folded) { 339db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { 3404f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman Constant *N = ConstantInt::get(DestTy, ATy->getNumElements()); 3414f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman Constant *E = getFoldedSizeOf(ATy->getElementType(), DestTy, true); 3424f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman return ConstantExpr::getNUWMul(E, N); 3434f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman } 3442440cf1c6f8efe83772dabc4582e8c1e3637cc56Dan Gohman 345db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner if (StructType *STy = dyn_cast<StructType>(Ty)) 3464f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman if (!STy->isPacked()) { 3474f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman unsigned NumElems = STy->getNumElements(); 3484f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman // An empty struct has size zero. 3494f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman if (NumElems == 0) 3504f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman return ConstantExpr::getNullValue(DestTy); 3516acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman // Check for a struct with all members having the same size. 3526acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman Constant *MemberSize = 3536acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman getFoldedSizeOf(STy->getElementType(0), DestTy, true); 3544f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman bool AllSame = true; 3554f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman for (unsigned i = 1; i != NumElems; ++i) 3566acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman if (MemberSize != 3576acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman getFoldedSizeOf(STy->getElementType(i), DestTy, true)) { 3584f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman AllSame = false; 3594f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman break; 3604f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman } 3614f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman if (AllSame) { 3624f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman Constant *N = ConstantInt::get(DestTy, NumElems); 3636acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman return ConstantExpr::getNUWMul(MemberSize, N); 3644f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman } 3654f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman } 3664f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman 367a84ffedafc102013a935817f30485a47d3dc2383Dan Gohman // Pointer size doesn't depend on the pointee type, so canonicalize them 368a84ffedafc102013a935817f30485a47d3dc2383Dan Gohman // to an arbitrary pointee. 369db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner if (PointerType *PTy = dyn_cast<PointerType>(Ty)) 370b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands if (!PTy->getElementType()->isIntegerTy(1)) 371a84ffedafc102013a935817f30485a47d3dc2383Dan Gohman return 372a84ffedafc102013a935817f30485a47d3dc2383Dan Gohman getFoldedSizeOf(PointerType::get(IntegerType::get(PTy->getContext(), 1), 373a84ffedafc102013a935817f30485a47d3dc2383Dan Gohman PTy->getAddressSpace()), 374a84ffedafc102013a935817f30485a47d3dc2383Dan Gohman DestTy, true); 375a84ffedafc102013a935817f30485a47d3dc2383Dan Gohman 3764f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman // If there's no interesting folding happening, bail so that we don't create 3774f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman // a constant that looks like it needs folding but really doesn't. 3784f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman if (!Folded) 3794f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman return 0; 3804f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman 3814f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman // Base case: Get a regular sizeof expression. 3824f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman Constant *C = ConstantExpr::getSizeOf(Ty); 3834f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, 3844f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman DestTy, false), 3854f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman C, DestTy); 3864f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman return C; 3874f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman} 3884f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman 3896acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman/// getFoldedAlignOf - Return a ConstantExpr with type DestTy for alignof 3906acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman/// on Ty, with any known factors factored out. If Folded is false, 3916acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman/// return null if no factoring was possible, to avoid endlessly 3926acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman/// bouncing an unfoldable expression back into the top-level folder. 3936acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman/// 394db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattnerstatic Constant *getFoldedAlignOf(Type *Ty, Type *DestTy, 3956acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman bool Folded) { 3966acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman // The alignment of an array is equal to the alignment of the 3976acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman // array element. Note that this is not always true for vectors. 398db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { 3996acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman Constant *C = ConstantExpr::getAlignOf(ATy->getElementType()); 4006acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, 4016acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman DestTy, 4026acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman false), 4036acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman C, DestTy); 4046acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman return C; 4056acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman } 4066acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman 407db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner if (StructType *STy = dyn_cast<StructType>(Ty)) { 4086acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman // Packed structs always have an alignment of 1. 4096acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman if (STy->isPacked()) 4106acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman return ConstantInt::get(DestTy, 1); 4116acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman 4126acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman // Otherwise, struct alignment is the maximum alignment of any member. 4136acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman // Without target data, we can't compare much, but we can check to see 4146acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman // if all the members have the same alignment. 4156acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman unsigned NumElems = STy->getNumElements(); 4166acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman // An empty struct has minimal alignment. 4176acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman if (NumElems == 0) 4186acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman return ConstantInt::get(DestTy, 1); 4196acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman // Check for a struct with all members having the same alignment. 4206acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman Constant *MemberAlign = 4216acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman getFoldedAlignOf(STy->getElementType(0), DestTy, true); 4226acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman bool AllSame = true; 4236acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman for (unsigned i = 1; i != NumElems; ++i) 4246acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman if (MemberAlign != getFoldedAlignOf(STy->getElementType(i), DestTy, true)) { 4256acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman AllSame = false; 4269ee0d8a917a90380afc65c5b8fcea9d9975e6749Dan Gohman break; 4279ee0d8a917a90380afc65c5b8fcea9d9975e6749Dan Gohman } 4289ee0d8a917a90380afc65c5b8fcea9d9975e6749Dan Gohman if (AllSame) 4299ee0d8a917a90380afc65c5b8fcea9d9975e6749Dan Gohman return MemberAlign; 4309ee0d8a917a90380afc65c5b8fcea9d9975e6749Dan Gohman } 4319ee0d8a917a90380afc65c5b8fcea9d9975e6749Dan Gohman 432a84ffedafc102013a935817f30485a47d3dc2383Dan Gohman // Pointer alignment doesn't depend on the pointee type, so canonicalize them 433a84ffedafc102013a935817f30485a47d3dc2383Dan Gohman // to an arbitrary pointee. 434db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner if (PointerType *PTy = dyn_cast<PointerType>(Ty)) 435b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands if (!PTy->getElementType()->isIntegerTy(1)) 436a84ffedafc102013a935817f30485a47d3dc2383Dan Gohman return 437a84ffedafc102013a935817f30485a47d3dc2383Dan Gohman getFoldedAlignOf(PointerType::get(IntegerType::get(PTy->getContext(), 438a84ffedafc102013a935817f30485a47d3dc2383Dan Gohman 1), 439a84ffedafc102013a935817f30485a47d3dc2383Dan Gohman PTy->getAddressSpace()), 440a84ffedafc102013a935817f30485a47d3dc2383Dan Gohman DestTy, true); 441a84ffedafc102013a935817f30485a47d3dc2383Dan Gohman 4426acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman // If there's no interesting folding happening, bail so that we don't create 4436acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman // a constant that looks like it needs folding but really doesn't. 4446acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman if (!Folded) 4456acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman return 0; 4466acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman 4476acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman // Base case: Get a regular alignof expression. 4486acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman Constant *C = ConstantExpr::getAlignOf(Ty); 4496acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, 4506acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman DestTy, false), 4516acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman C, DestTy); 4526acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman return C; 4536acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman} 4546acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman 4554f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman/// getFoldedOffsetOf - Return a ConstantExpr with type DestTy for offsetof 4564f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman/// on Ty and FieldNo, with any known factors factored out. If Folded is false, 4574f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman/// return null if no factoring was possible, to avoid endlessly 4584f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman/// bouncing an unfoldable expression back into the top-level folder. 4594f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman/// 460db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattnerstatic Constant *getFoldedOffsetOf(Type *Ty, Constant *FieldNo, 461db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *DestTy, 4624f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman bool Folded) { 463db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { 4644f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman Constant *N = ConstantExpr::getCast(CastInst::getCastOpcode(FieldNo, false, 4654f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman DestTy, false), 4664f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman FieldNo, DestTy); 4674f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman Constant *E = getFoldedSizeOf(ATy->getElementType(), DestTy, true); 4684f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman return ConstantExpr::getNUWMul(E, N); 4694f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman } 4702440cf1c6f8efe83772dabc4582e8c1e3637cc56Dan Gohman 471db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner if (StructType *STy = dyn_cast<StructType>(Ty)) 4724f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman if (!STy->isPacked()) { 4734f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman unsigned NumElems = STy->getNumElements(); 4744f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman // An empty struct has no members. 4754f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman if (NumElems == 0) 4764f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman return 0; 4776acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman // Check for a struct with all members having the same size. 4786acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman Constant *MemberSize = 4796acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman getFoldedSizeOf(STy->getElementType(0), DestTy, true); 4804f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman bool AllSame = true; 4814f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman for (unsigned i = 1; i != NumElems; ++i) 4826acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman if (MemberSize != 4836acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman getFoldedSizeOf(STy->getElementType(i), DestTy, true)) { 4844f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman AllSame = false; 4854f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman break; 4864f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman } 4874f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman if (AllSame) { 4884f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman Constant *N = ConstantExpr::getCast(CastInst::getCastOpcode(FieldNo, 4894f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman false, 4904f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman DestTy, 4914f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman false), 4924f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman FieldNo, DestTy); 4936acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman return ConstantExpr::getNUWMul(MemberSize, N); 4944f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman } 4954f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman } 4964f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman 4974f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman // If there's no interesting folding happening, bail so that we don't create 4984f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman // a constant that looks like it needs folding but really doesn't. 4994f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman if (!Folded) 5004f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman return 0; 5014f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman 5024f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman // Base case: Get a regular offsetof expression. 5034f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman Constant *C = ConstantExpr::getOffsetOf(Ty, FieldNo); 5044f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, 5054f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman DestTy, false), 5064f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman C, DestTy); 5074f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman return C; 5084f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman} 50959c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner 510b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris LattnerConstant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V, 511db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *DestTy) { 512d2f09965e6ba462c4728ff4d5c79b0d0d20ec47fChris Lattner if (isa<UndefValue>(V)) { 513d2f09965e6ba462c4728ff4d5c79b0d0d20ec47fChris Lattner // zext(undef) = 0, because the top bits will be zero. 514d2f09965e6ba462c4728ff4d5c79b0d0d20ec47fChris Lattner // sext(undef) = 0, because the top bits will all be the same. 51546acf85b97534fcf9997286da87c38210ce937ecChris Lattner // [us]itofp(undef) = 0, because the result value is bounded. 51646acf85b97534fcf9997286da87c38210ce937ecChris Lattner if (opc == Instruction::ZExt || opc == Instruction::SExt || 51746acf85b97534fcf9997286da87c38210ce937ecChris Lattner opc == Instruction::UIToFP || opc == Instruction::SIToFP) 518a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson return Constant::getNullValue(DestTy); 5199e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson return UndefValue::get(DestTy); 520d2f09965e6ba462c4728ff4d5c79b0d0d20ec47fChris Lattner } 5214c93a0f09225b428fb0f28f1c540f5ff5dd5bb3cNick Lewycky 5224c93a0f09225b428fb0f28f1c540f5ff5dd5bb3cNick Lewycky if (V->isNullValue() && !DestTy->isX86_MMXTy()) 5234c93a0f09225b428fb0f28f1c540f5ff5dd5bb3cNick Lewycky return Constant::getNullValue(DestTy); 5244c93a0f09225b428fb0f28f1c540f5ff5dd5bb3cNick Lewycky 5253da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer // If the cast operand is a constant expression, there's a few things we can 5263da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer // do to try to simplify it. 52733c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { 5283da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer if (CE->isCast()) { 529575d95ce373f1e405e6c27ce8d9f244bba3bdd0dReid Spencer // Try hard to fold cast of cast because they are often eliminable. 5303da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy)) 531baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy); 532eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner } else if (CE->getOpcode() == Instruction::GetElementPtr) { 533eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner // If all of the indexes in the GEP are null values, there is no pointer 534eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner // adjustment going on. We might as well cast the source pointer. 535eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner bool isAllNull = true; 536eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) 537eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner if (!CE->getOperand(i)->isNullValue()) { 538eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner isAllNull = false; 539eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner break; 540eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner } 541eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner if (isAllNull) 542575d95ce373f1e405e6c27ce8d9f244bba3bdd0dReid Spencer // This is casting one pointer type to another, always BitCast 543baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy); 544eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner } 54571d3778c0b75a637c66704689bf624e9bd317521Chris Lattner } 546eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner 5476de29f8d960505421d61c80cdb738e16720b6c0eDan Gohman // If the cast operand is a constant vector, perform the cast by 5486de29f8d960505421d61c80cdb738e16720b6c0eDan Gohman // operating on each element. In the cast of bitcasts, the element 5496de29f8d960505421d61c80cdb738e16720b6c0eDan Gohman // count may be mismatched; don't attempt to handle that here. 550a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7aChris Lattner if ((isa<ConstantVector>(V) || isa<ConstantDataVector>(V)) && 551a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7aChris Lattner DestTy->isVectorTy() && 552a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7aChris Lattner DestTy->getVectorNumElements() == V->getType()->getVectorNumElements()) { 553a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7aChris Lattner SmallVector<Constant*, 16> res; 554a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7aChris Lattner VectorType *DestVecTy = cast<VectorType>(DestTy); 555a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7aChris Lattner Type *DstEltTy = DestVecTy->getElementType(); 55603e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman Type *Ty = IntegerType::get(V->getContext(), 32); 55703e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman for (unsigned i = 0, e = V->getType()->getVectorNumElements(); i != e; ++i) { 55803e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman Constant *C = 55903e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman ConstantExpr::getExtractElement(V, ConstantInt::get(Ty, i)); 56003e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman res.push_back(ConstantExpr::getCast(opc, C, DstEltTy)); 56103e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman } 562a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7aChris Lattner return ConstantVector::get(res); 563a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7aChris Lattner } 5646de29f8d960505421d61c80cdb738e16720b6c0eDan Gohman 565390437fc6d6e7a244e7c81683576e7d27a425ceaReid Spencer // We actually have to do a cast now. Perform the cast according to the 566390437fc6d6e7a244e7c81683576e7d27a425ceaReid Spencer // opcode specified. 5673da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer switch (opc) { 56859c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner default: 56959c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner llvm_unreachable("Failed to cast constant expression"); 5703da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer case Instruction::FPTrunc: 5713da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer case Instruction::FPExt: 57233c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) { 57323a98551ab65eeb8fe5019df8b7db4891582a4bdDale Johannesen bool ignored; 574ac1220551fa3a4dc8b1eea2584920b8f18d7b571Dale Johannesen APFloat Val = FPC->getValueAPF(); 575ce16339930a2b03e53b4e6399ef59c092a7f2cfaDan Gohman Val.convert(DestTy->isHalfTy() ? APFloat::IEEEhalf : 576ce16339930a2b03e53b4e6399ef59c092a7f2cfaDan Gohman DestTy->isFloatTy() ? APFloat::IEEEsingle : 577cf0fe8d813727383d630055bb9d1cde21b00b7e7Chris Lattner DestTy->isDoubleTy() ? APFloat::IEEEdouble : 578cf0fe8d813727383d630055bb9d1cde21b00b7e7Chris Lattner DestTy->isX86_FP80Ty() ? APFloat::x87DoubleExtended : 579cf0fe8d813727383d630055bb9d1cde21b00b7e7Chris Lattner DestTy->isFP128Ty() ? APFloat::IEEEquad : 5803467b9fd5d0c4ebc726e96c2af96610e2d6915f9Ulrich Weigand DestTy->isPPC_FP128Ty() ? APFloat::PPCDoubleDouble : 581ac1220551fa3a4dc8b1eea2584920b8f18d7b571Dale Johannesen APFloat::Bogus, 58223a98551ab65eeb8fe5019df8b7db4891582a4bdDale Johannesen APFloat::rmNearestTiesToEven, &ignored); 583b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantFP::get(V->getContext(), Val); 58443421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen } 58585e36e474d795c95fd784e6312d3ef2fbfc0c99eReid Spencer return 0; // Can't fold. 58685e36e474d795c95fd784e6312d3ef2fbfc0c99eReid Spencer case Instruction::FPToUI: 58785e36e474d795c95fd784e6312d3ef2fbfc0c99eReid Spencer case Instruction::FPToSI: 58833c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (ConstantFP *FPC = dyn_cast<ConstantFP>(V)) { 5893cab1c72b54cceaf2a4275dabf1b4160765820fcChris Lattner const APFloat &V = FPC->getValueAPF(); 59023a98551ab65eeb8fe5019df8b7db4891582a4bdDale Johannesen bool ignored; 591ac1220551fa3a4dc8b1eea2584920b8f18d7b571Dale Johannesen uint64_t x[2]; 5929472c373a3378982ab4ef1a2caafabd4acf58ba2Reid Spencer uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth(); 593350add8f510f54f7728721d15e9c319c47dbe55bDale Johannesen (void) V.convertToInteger(x, DestBitWidth, opc==Instruction::FPToSI, 59423a98551ab65eeb8fe5019df8b7db4891582a4bdDale Johannesen APFloat::rmTowardZero, &ignored); 5953ba292dbc2acee2d1052fb7ffe332e2164147b47Jeffrey Yasskin APInt Val(DestBitWidth, x); 596b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(FPC->getContext(), Val); 5979472c373a3378982ab4ef1a2caafabd4acf58ba2Reid Spencer } 59885e36e474d795c95fd784e6312d3ef2fbfc0c99eReid Spencer return 0; // Can't fold. 59985e36e474d795c95fd784e6312d3ef2fbfc0c99eReid Spencer case Instruction::IntToPtr: //always treated as unsigned 60085e36e474d795c95fd784e6312d3ef2fbfc0c99eReid Spencer if (V->isNullValue()) // Is it an integral null value? 6019e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson return ConstantPointerNull::get(cast<PointerType>(DestTy)); 60285e36e474d795c95fd784e6312d3ef2fbfc0c99eReid Spencer return 0; // Other pointer types cannot be casted 60385e36e474d795c95fd784e6312d3ef2fbfc0c99eReid Spencer case Instruction::PtrToInt: // always treated as unsigned 6040f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman // Is it a null pointer value? 6050f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman if (V->isNullValue()) 606eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson return ConstantInt::get(DestTy, 0); 6074f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman // If this is a sizeof-like expression, pull out multiplications by 6084f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman // known factors to expose them to subsequent folding. If it's an 6094f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman // alignof-like expression, factor out known factors. 6100f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) 6110f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman if (CE->getOpcode() == Instruction::GetElementPtr && 6124f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman CE->getOperand(0)->isNullValue()) { 613db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *Ty = 6144f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman cast<PointerType>(CE->getOperand(0)->getType())->getElementType(); 6154f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman if (CE->getNumOperands() == 2) { 6164f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman // Handle a sizeof-like expression. 6174f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman Constant *Idx = CE->getOperand(1); 6184f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman bool isOne = isa<ConstantInt>(Idx) && cast<ConstantInt>(Idx)->isOne(); 6194f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman if (Constant *C = getFoldedSizeOf(Ty, DestTy, !isOne)) { 6204f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman Idx = ConstantExpr::getCast(CastInst::getCastOpcode(Idx, true, 6210f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman DestTy, false), 6224f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman Idx, DestTy); 6234f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman return ConstantExpr::getMul(C, Idx); 6244f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman } 6254f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman } else if (CE->getNumOperands() == 3 && 6264f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman CE->getOperand(1)->isNullValue()) { 6274f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman // Handle an alignof-like expression. 628db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner if (StructType *STy = dyn_cast<StructType>(Ty)) 6294f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman if (!STy->isPacked()) { 6304f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman ConstantInt *CI = cast<ConstantInt>(CE->getOperand(2)); 6314f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman if (CI->isOne() && 6324f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman STy->getNumElements() == 2 && 633b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands STy->getElementType(0)->isIntegerTy(1)) { 6346acb86dcfa1ff4e38d2f189ab09e3fbe22f248a7Dan Gohman return getFoldedAlignOf(STy->getElementType(1), DestTy, false); 6354f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman } 6360f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman } 6374f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman // Handle an offsetof-like expression. 638fdbef823ad58f4ddb96da61e59ca48d8390c928fDan Gohman if (Ty->isStructTy() || Ty->isArrayTy()) { 6394f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman if (Constant *C = getFoldedOffsetOf(Ty, CE->getOperand(2), 6404f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman DestTy, false)) 6414f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman return C; 6420f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman } 6434f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman } 6444f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman } 6450f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman // Other pointer types cannot be casted 6460f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman return 0; 64785e36e474d795c95fd784e6312d3ef2fbfc0c99eReid Spencer case Instruction::UIToFP: 64885e36e474d795c95fd784e6312d3ef2fbfc0c99eReid Spencer case Instruction::SIToFP: 64933c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 65088216af3ea4bb1c68a8793ed1d3b30308b64ab0eDale Johannesen APInt api = CI->getValue(); 6510a29cb045444c13160e90fe7942a9d7c720185edTim Northover APFloat apf(DestTy->getFltSemantics(), 6520a29cb045444c13160e90fe7942a9d7c720185edTim Northover APInt::getNullValue(DestTy->getPrimitiveSizeInBits())); 65337a9ca5bc580e2a8c602a819a0ebdee1d205f1afDan Gohman (void)apf.convertFromAPInt(api, 65437a9ca5bc580e2a8c602a819a0ebdee1d205f1afDan Gohman opc==Instruction::SIToFP, 65537a9ca5bc580e2a8c602a819a0ebdee1d205f1afDan Gohman APFloat::rmNearestTiesToEven); 656b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantFP::get(V->getContext(), apf); 65743421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen } 65885e36e474d795c95fd784e6312d3ef2fbfc0c99eReid Spencer return 0; 6593da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer case Instruction::ZExt: 66033c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 6619472c373a3378982ab4ef1a2caafabd4acf58ba2Reid Spencer uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth(); 66240f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad return ConstantInt::get(V->getContext(), 66340f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad CI->getValue().zext(BitWidth)); 6649472c373a3378982ab4ef1a2caafabd4acf58ba2Reid Spencer } 665390437fc6d6e7a244e7c81683576e7d27a425ceaReid Spencer return 0; 6663da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer case Instruction::SExt: 66733c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 6689472c373a3378982ab4ef1a2caafabd4acf58ba2Reid Spencer uint32_t BitWidth = cast<IntegerType>(DestTy)->getBitWidth(); 66940f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad return ConstantInt::get(V->getContext(), 67040f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad CI->getValue().sext(BitWidth)); 6719472c373a3378982ab4ef1a2caafabd4acf58ba2Reid Spencer } 672390437fc6d6e7a244e7c81683576e7d27a425ceaReid Spencer return 0; 67359c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner case Instruction::Trunc: { 67459c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner uint32_t DestBitWidth = cast<IntegerType>(DestTy)->getBitWidth(); 67533c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { 67640f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad return ConstantInt::get(V->getContext(), 67740f8f6264d5af2c38e797e0dc59827cd231e8ff7Jay Foad CI->getValue().trunc(DestBitWidth)); 6789472c373a3378982ab4ef1a2caafabd4acf58ba2Reid Spencer } 67959c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner 68059c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner // The input must be a constantexpr. See if we can simplify this based on 68159c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner // the bytes we are demanding. Only do this if the source and dest are an 68259c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner // even multiple of a byte. 68359c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if ((DestBitWidth & 7) == 0 && 68459c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner (cast<IntegerType>(V->getType())->getBitWidth() & 7) == 0) 68559c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner if (Constant *Res = ExtractConstantBytes(V, 0, DestBitWidth / 8)) 68659c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner return Res; 68759c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner 6885be662505e16c485596b53a8eabc3edb8bc88d17Chris Lattner return 0; 68959c4eba4169e40b36719c521c25d29115ff6b206Chris Lattner } 6903da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer case Instruction::BitCast: 691b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return FoldBitCast(V, DestTy); 6924460f4058fb33e468358f85c7f0925fc0e69e16cChris Lattner } 693eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner} 694eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner 695b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris LattnerConstant *llvm::ConstantFoldSelectInstruction(Constant *Cond, 69633c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky Constant *V1, Constant *V2) { 697d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner // Check for i1 and vector true/false conditions. 698d2f27ead2d71afeee869cad8ae8a1c1dce7229cbNadav Rotem if (Cond->isNullValue()) return V2; 699d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner if (Cond->isAllOnesValue()) return V1; 700d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner 701d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner // If the condition is a vector constant, fold the result elementwise. 702d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner if (ConstantVector *CondV = dyn_cast<ConstantVector>(Cond)) { 703d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner SmallVector<Constant*, 16> Result; 70403e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman Type *Ty = IntegerType::get(CondV->getContext(), 32); 705d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner for (unsigned i = 0, e = V1->getType()->getVectorNumElements(); i != e;++i){ 706d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner ConstantInt *Cond = dyn_cast<ConstantInt>(CondV->getOperand(i)); 707d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner if (Cond == 0) break; 708d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner 70903e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman Constant *V = Cond->isNullValue() ? V2 : V1; 71003e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman Constant *Res = ConstantExpr::getExtractElement(V, ConstantInt::get(Ty, i)); 711d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner Result.push_back(Res); 712d2f27ead2d71afeee869cad8ae8a1c1dce7229cbNadav Rotem } 713d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner 714d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner // If we were able to build the vector, return it. 715d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner if (Result.size() == V1->getType()->getVectorNumElements()) 716d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner return ConstantVector::get(Result); 717d2f27ead2d71afeee869cad8ae8a1c1dce7229cbNadav Rotem } 718d2f27ead2d71afeee869cad8ae8a1c1dce7229cbNadav Rotem 71968c0dbc14fb7599987fb3e27be4e12c1ac36535eDan Gohman if (isa<UndefValue>(Cond)) { 72068c0dbc14fb7599987fb3e27be4e12c1ac36535eDan Gohman if (isa<UndefValue>(V1)) return V1; 72168c0dbc14fb7599987fb3e27be4e12c1ac36535eDan Gohman return V2; 72268c0dbc14fb7599987fb3e27be4e12c1ac36535eDan Gohman } 72333c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (isa<UndefValue>(V1)) return V2; 72433c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (isa<UndefValue>(V2)) return V1; 72533c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (V1 == V2) return V1; 726175e7aec1347be3469b0838e5380d424228aa48fNick Lewycky 727175e7aec1347be3469b0838e5380d424228aa48fNick Lewycky if (ConstantExpr *TrueVal = dyn_cast<ConstantExpr>(V1)) { 728175e7aec1347be3469b0838e5380d424228aa48fNick Lewycky if (TrueVal->getOpcode() == Instruction::Select) 729175e7aec1347be3469b0838e5380d424228aa48fNick Lewycky if (TrueVal->getOperand(0) == Cond) 73056cb2298663017eb77aa4f4dda8db7ecd1b58173Bill Wendling return ConstantExpr::getSelect(Cond, TrueVal->getOperand(1), V2); 731175e7aec1347be3469b0838e5380d424228aa48fNick Lewycky } 732175e7aec1347be3469b0838e5380d424228aa48fNick Lewycky if (ConstantExpr *FalseVal = dyn_cast<ConstantExpr>(V2)) { 733175e7aec1347be3469b0838e5380d424228aa48fNick Lewycky if (FalseVal->getOpcode() == Instruction::Select) 734175e7aec1347be3469b0838e5380d424228aa48fNick Lewycky if (FalseVal->getOperand(0) == Cond) 73556cb2298663017eb77aa4f4dda8db7ecd1b58173Bill Wendling return ConstantExpr::getSelect(Cond, V1, FalseVal->getOperand(2)); 736175e7aec1347be3469b0838e5380d424228aa48fNick Lewycky } 737175e7aec1347be3469b0838e5380d424228aa48fNick Lewycky 738e97148628f8a52fff1752937f6f49945bbe9937bChris Lattner return 0; 739e97148628f8a52fff1752937f6f49945bbe9937bChris Lattner} 740e97148628f8a52fff1752937f6f49945bbe9937bChris Lattner 741b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris LattnerConstant *llvm::ConstantFoldExtractElementInstruction(Constant *Val, 74233c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky Constant *Idx) { 7436fa4cdf92eb240f06c7705a8d72b408668b5869bChris Lattner if (isa<UndefValue>(Val)) // ee(undef, x) -> undef 744a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7aChris Lattner return UndefValue::get(Val->getType()->getVectorElementType()); 745f38d47144572275396e6e6611fa5e181e79b2944Chris Lattner if (Val->isNullValue()) // ee(zero, x) -> zero 746a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7aChris Lattner return Constant::getNullValue(Val->getType()->getVectorElementType()); 747a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7aChris Lattner // ee({w,x,y,z}, undef) -> undef 748a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7aChris Lattner if (isa<UndefValue>(Idx)) 749a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7aChris Lattner return UndefValue::get(Val->getType()->getVectorElementType()); 750a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7aChris Lattner 751a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7aChris Lattner if (ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) { 752a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7aChris Lattner uint64_t Index = CIdx->getZExtValue(); 753a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7aChris Lattner // ee({w,x,y,z}, wrong_value) -> undef 754a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7aChris Lattner if (Index >= Val->getType()->getVectorNumElements()) 755a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7aChris Lattner return UndefValue::get(Val->getType()->getVectorElementType()); 756a78fa8cc2dd6d2ffe5e4fe605f38aae7b3d2fb7aChris Lattner return Val->getAggregateElement(Index); 7576fa4cdf92eb240f06c7705a8d72b408668b5869bChris Lattner } 758bb90a7aa7bf71311046ccc9f277e5f76cc082722Robert Bocchino return 0; 759bb90a7aa7bf71311046ccc9f277e5f76cc082722Robert Bocchino} 760bb90a7aa7bf71311046ccc9f277e5f76cc082722Robert Bocchino 761b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris LattnerConstant *llvm::ConstantFoldInsertElementInstruction(Constant *Val, 76233c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky Constant *Elt, 76333c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky Constant *Idx) { 76433c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx); 765c152f9cd26e7cb32352c513389a18ffd892ecaecRobert Bocchino if (!CIdx) return 0; 766d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner const APInt &IdxVal = CIdx->getValue(); 767d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner 768d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner SmallVector<Constant*, 16> Result; 76903e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman Type *Ty = IntegerType::get(Val->getContext(), 32); 770d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner for (unsigned i = 0, e = Val->getType()->getVectorNumElements(); i != e; ++i){ 771d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner if (i == IdxVal) { 772d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner Result.push_back(Elt); 773d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner continue; 774c152f9cd26e7cb32352c513389a18ffd892ecaecRobert Bocchino } 775d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner 77603e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman Constant *C = 77703e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman ConstantExpr::getExtractElement(Val, ConstantInt::get(Ty, i)); 77803e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman Result.push_back(C); 779c152f9cd26e7cb32352c513389a18ffd892ecaecRobert Bocchino } 780d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner 781d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner return ConstantVector::get(Result); 7827431c2ba79cbf7019aafac2ebafa259621726be2Chris Lattner} 7837431c2ba79cbf7019aafac2ebafa259621726be2Chris Lattner 784b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris LattnerConstant *llvm::ConstantFoldShuffleVectorInstruction(Constant *V1, 78533c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky Constant *V2, 78633c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky Constant *Mask) { 7870cd0d8149278782aaa93c190f81e04f10a4efb5eChris Lattner unsigned MaskNumElts = Mask->getType()->getVectorNumElements(); 7880cd0d8149278782aaa93c190f81e04f10a4efb5eChris Lattner Type *EltTy = V1->getType()->getVectorElementType(); 7890cd0d8149278782aaa93c190f81e04f10a4efb5eChris Lattner 7907431c2ba79cbf7019aafac2ebafa259621726be2Chris Lattner // Undefined shuffle mask -> undefined value. 7910cd0d8149278782aaa93c190f81e04f10a4efb5eChris Lattner if (isa<UndefValue>(Mask)) 7920cd0d8149278782aaa93c190f81e04f10a4efb5eChris Lattner return UndefValue::get(VectorType::get(EltTy, MaskNumElts)); 793aeb06d246254e4829a49164a11eacced9a43d9d4Mon P Wang 79429bb00b3c5dda58dabb57d016bd0c4099d37cd3bChris Lattner // Don't break the bitcode reader hack. 79529bb00b3c5dda58dabb57d016bd0c4099d37cd3bChris Lattner if (isa<ConstantExpr>(Mask)) return 0; 79629bb00b3c5dda58dabb57d016bd0c4099d37cd3bChris Lattner 79771a494d6974f5105f008601a3e7c7474bf508139Chris Lattner unsigned SrcNumElts = V1->getType()->getVectorNumElements(); 798aeb06d246254e4829a49164a11eacced9a43d9d4Mon P Wang 7997431c2ba79cbf7019aafac2ebafa259621726be2Chris Lattner // Loop over the shuffle mask, evaluating each element. 8007431c2ba79cbf7019aafac2ebafa259621726be2Chris Lattner SmallVector<Constant*, 32> Result; 801aeb06d246254e4829a49164a11eacced9a43d9d4Mon P Wang for (unsigned i = 0; i != MaskNumElts; ++i) { 80271a494d6974f5105f008601a3e7c7474bf508139Chris Lattner int Elt = ShuffleVectorInst::getMaskValue(Mask, i); 80371a494d6974f5105f008601a3e7c7474bf508139Chris Lattner if (Elt == -1) { 804d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner Result.push_back(UndefValue::get(EltTy)); 805d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner continue; 8067431c2ba79cbf7019aafac2ebafa259621726be2Chris Lattner } 80771a494d6974f5105f008601a3e7c7474bf508139Chris Lattner Constant *InElt; 80871a494d6974f5105f008601a3e7c7474bf508139Chris Lattner if (unsigned(Elt) >= SrcNumElts*2) 809d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner InElt = UndefValue::get(EltTy); 81003e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman else if (unsigned(Elt) >= SrcNumElts) { 81103e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman Type *Ty = IntegerType::get(V2->getContext(), 32); 81203e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman InElt = 81303e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman ConstantExpr::getExtractElement(V2, 81403e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman ConstantInt::get(Ty, Elt - SrcNumElts)); 81503e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman } else { 81603e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman Type *Ty = IntegerType::get(V1->getContext(), 32); 81703e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman InElt = ConstantExpr::getExtractElement(V1, ConstantInt::get(Ty, Elt)); 81803e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman } 8197431c2ba79cbf7019aafac2ebafa259621726be2Chris Lattner Result.push_back(InElt); 8207431c2ba79cbf7019aafac2ebafa259621726be2Chris Lattner } 821aeb06d246254e4829a49164a11eacced9a43d9d4Mon P Wang 8222ca5c8644e6c35b3a7910a576ed89cddb7b82c3bChris Lattner return ConstantVector::get(Result); 82300f1023cf8b30c74dc219525f518a80c45b6e7baChris Lattner} 82400f1023cf8b30c74dc219525f518a80c45b6e7baChris Lattner 825b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris LattnerConstant *llvm::ConstantFoldExtractValueInstruction(Constant *Agg, 826fc6d3a49867cd38954dc40936a88f1907252c6d2Jay Foad ArrayRef<unsigned> Idxs) { 8276e68f59b78d8dcb3940469351a08d5884f190223Dan Gohman // Base case: no indices, so return the entire value. 828fc6d3a49867cd38954dc40936a88f1907252c6d2Jay Foad if (Idxs.empty()) 82933c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky return Agg; 8306e68f59b78d8dcb3940469351a08d5884f190223Dan Gohman 831d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner if (Constant *C = Agg->getAggregateElement(Idxs[0])) 832d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner return ConstantFoldExtractValueInstruction(C, Idxs.slice(1)); 833d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner 834d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner return 0; 835041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman} 836041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman 837b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris LattnerConstant *llvm::ConstantFoldInsertValueInstruction(Constant *Agg, 83833c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky Constant *Val, 839fc6d3a49867cd38954dc40936a88f1907252c6d2Jay Foad ArrayRef<unsigned> Idxs) { 8406e68f59b78d8dcb3940469351a08d5884f190223Dan Gohman // Base case: no indices, so replace the entire value. 841fc6d3a49867cd38954dc40936a88f1907252c6d2Jay Foad if (Idxs.empty()) 84233c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky return Val; 8436e68f59b78d8dcb3940469351a08d5884f190223Dan Gohman 844d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner unsigned NumElts; 845d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner if (StructType *ST = dyn_cast<StructType>(Agg->getType())) 846d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner NumElts = ST->getNumElements(); 847d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner else if (ArrayType *AT = dyn_cast<ArrayType>(Agg->getType())) 848d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner NumElts = AT->getNumElements(); 849d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner else 850a175396816a9b28835acfe2cd07250881f1fee6cNadav Rotem NumElts = Agg->getType()->getVectorNumElements(); 851a175396816a9b28835acfe2cd07250881f1fee6cNadav Rotem 852d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner SmallVector<Constant*, 32> Result; 853d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner for (unsigned i = 0; i != NumElts; ++i) { 854d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner Constant *C = Agg->getAggregateElement(i); 855d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner if (C == 0) return 0; 856dd4238e04dc8a5144d9c19bd53cf9650d1472309Chris Lattner 857d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner if (Idxs[0] == i) 858d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner C = ConstantFoldInsertValueInstruction(C, Val, Idxs.slice(1)); 859dd4238e04dc8a5144d9c19bd53cf9650d1472309Chris Lattner 860d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner Result.push_back(C); 8616e68f59b78d8dcb3940469351a08d5884f190223Dan Gohman } 862dd4238e04dc8a5144d9c19bd53cf9650d1472309Chris Lattner 863d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner if (StructType *ST = dyn_cast<StructType>(Agg->getType())) 864d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner return ConstantStruct::get(ST, Result); 865d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner if (ArrayType *AT = dyn_cast<ArrayType>(Agg->getType())) 866d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner return ConstantArray::get(AT, Result); 867d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner return ConstantVector::get(Result); 868041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman} 869041e2eb51721bcfecee5d9c9fc409ff185526e47Dan Gohman 870e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer 871b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris LattnerConstant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, 87233c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky Constant *C1, Constant *C2) { 873dd4238e04dc8a5144d9c19bd53cf9650d1472309Chris Lattner // Handle UndefValue up front. 874e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) { 875e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer switch (Opcode) { 87626471c48b3d49bdbcfcc05cb9a575b5fa123fbbfEvan Cheng case Instruction::Xor: 87726471c48b3d49bdbcfcc05cb9a575b5fa123fbbfEvan Cheng if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) 87826471c48b3d49bdbcfcc05cb9a575b5fa123fbbfEvan Cheng // Handle undef ^ undef -> 0 special case. This is a common 87926471c48b3d49bdbcfcc05cb9a575b5fa123fbbfEvan Cheng // idiom (misuse). 880a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson return Constant::getNullValue(C1->getType()); 88126471c48b3d49bdbcfcc05cb9a575b5fa123fbbfEvan Cheng // Fallthrough 882e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case Instruction::Add: 883e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case Instruction::Sub: 8849e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson return UndefValue::get(C1->getType()); 885e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case Instruction::And: 88630cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef & undef -> undef 88730cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman return C1; 88830cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman return Constant::getNullValue(C1->getType()); // undef & X -> 0 88930cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman case Instruction::Mul: { 89030cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman ConstantInt *CI; 89130cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman // X * undef -> undef if X is odd or undef 89230cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman if (((CI = dyn_cast<ConstantInt>(C1)) && CI->getValue()[0]) || 89330cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman ((CI = dyn_cast<ConstantInt>(C2)) && CI->getValue()[0]) || 89430cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman (isa<UndefValue>(C1) && isa<UndefValue>(C2))) 89530cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman return UndefValue::get(C1->getType()); 89630cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman 89730cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman // X * undef -> 0 otherwise 898a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson return Constant::getNullValue(C1->getType()); 89930cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman } 900e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case Instruction::UDiv: 901e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case Instruction::SDiv: 90230cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman // undef / 1 -> undef 90330cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman if (Opcode == Instruction::UDiv || Opcode == Instruction::SDiv) 90430cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) 90530cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman if (CI2->isOne()) 90630cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman return C1; 90730cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman // FALL THROUGH 908e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case Instruction::URem: 909e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case Instruction::SRem: 910e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer if (!isa<UndefValue>(C2)) // undef / X -> 0 911a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson return Constant::getNullValue(C1->getType()); 91233c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky return C2; // X / undef -> undef 913e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case Instruction::Or: // X | undef -> -1 91430cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman if (isa<UndefValue>(C1) && isa<UndefValue>(C2)) // undef | undef -> undef 91530cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman return C1; 91630cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0 917e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case Instruction::LShr: 918e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer if (isa<UndefValue>(C2) && isa<UndefValue>(C1)) 91933c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky return C1; // undef lshr undef -> undef 920a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson return Constant::getNullValue(C1->getType()); // X lshr undef -> 0 921e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer // undef lshr X -> 0 922e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case Instruction::AShr: 923c43cee3fbb3098f0647e50dd2c13bc55b027a228Duncan Sands if (!isa<UndefValue>(C2)) // undef ashr X --> all ones 924c43cee3fbb3098f0647e50dd2c13bc55b027a228Duncan Sands return Constant::getAllOnesValue(C1->getType()); 925e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer else if (isa<UndefValue>(C1)) 92633c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky return C1; // undef ashr undef -> undef 927e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer else 92833c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky return C1; // X ashr undef --> X 929e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case Instruction::Shl: 93030cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman if (isa<UndefValue>(C2) && isa<UndefValue>(C1)) 93130cb6dda5ae441f27edef302cbb33936fbafba6dDan Gohman return C1; // undef shl undef -> undef 932e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer // undef << X -> 0 or X << undef -> 0 933a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson return Constant::getNullValue(C1->getType()); 934e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer } 935e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer } 936e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer 937e47f59db448fd468bb32ade37a1fa7ee8a9bff20Nick Lewycky // Handle simplifications when the RHS is a constant int. 93833c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) { 9391c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner switch (Opcode) { 9401c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner case Instruction::Add: 94133c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (CI2->equalsInt(0)) return C1; // X + 0 == X 9421c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner break; 9431c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner case Instruction::Sub: 94433c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (CI2->equalsInt(0)) return C1; // X - 0 == X 9451c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner break; 9461c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner case Instruction::Mul: 94733c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (CI2->equalsInt(0)) return C2; // X * 0 == 0 9481c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner if (CI2->equalsInt(1)) 94933c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky return C1; // X * 1 == X 9501c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner break; 9511c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner case Instruction::UDiv: 9521c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner case Instruction::SDiv: 9531c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner if (CI2->equalsInt(1)) 95433c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky return C1; // X / 1 == X 955c9a005807ab46d03a3b24f84220542c6eb84e9d1Chris Lattner if (CI2->equalsInt(0)) 9569e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson return UndefValue::get(CI2->getType()); // X / 0 == undef 9571c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner break; 9581c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner case Instruction::URem: 9591c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner case Instruction::SRem: 9601c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner if (CI2->equalsInt(1)) 961a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson return Constant::getNullValue(CI2->getType()); // X % 1 == 0 962c9a005807ab46d03a3b24f84220542c6eb84e9d1Chris Lattner if (CI2->equalsInt(0)) 9639e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson return UndefValue::get(CI2->getType()); // X % 0 == undef 9641c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner break; 9651c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner case Instruction::And: 96633c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (CI2->isZero()) return C2; // X & 0 == 0 9671c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner if (CI2->isAllOnesValue()) 96833c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky return C1; // X & -1 == X 969f8a87e8343c5f829be9a75d8160e1bb969b84119Dan Gohman 97033c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) { 971d881ad2c570dcb5965eb00b66462e1075ec82440Chris Lattner // (zext i32 to i64) & 4294967295 -> (zext i32 to i64) 9721c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner if (CE1->getOpcode() == Instruction::ZExt) { 9731c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner unsigned DstWidth = CI2->getType()->getBitWidth(); 9741c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner unsigned SrcWidth = 9751c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner CE1->getOperand(0)->getType()->getPrimitiveSizeInBits(); 9761c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner APInt PossiblySetBits(APInt::getLowBitsSet(DstWidth, SrcWidth)); 9771c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner if ((PossiblySetBits & CI2->getValue()) == PossiblySetBits) 97833c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky return C1; 9792ef14d9f7c416105be650021c8010bcf47583065Chris Lattner } 980f8a87e8343c5f829be9a75d8160e1bb969b84119Dan Gohman 981d881ad2c570dcb5965eb00b66462e1075ec82440Chris Lattner // If and'ing the address of a global with a constant, fold it. 9821c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner if (CE1->getOpcode() == Instruction::PtrToInt && 9831c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner isa<GlobalValue>(CE1->getOperand(0))) { 984d881ad2c570dcb5965eb00b66462e1075ec82440Chris Lattner GlobalValue *GV = cast<GlobalValue>(CE1->getOperand(0)); 985f8a87e8343c5f829be9a75d8160e1bb969b84119Dan Gohman 986d881ad2c570dcb5965eb00b66462e1075ec82440Chris Lattner // Functions are at least 4-byte aligned. 987d881ad2c570dcb5965eb00b66462e1075ec82440Chris Lattner unsigned GVAlign = GV->getAlignment(); 988d881ad2c570dcb5965eb00b66462e1075ec82440Chris Lattner if (isa<Function>(GV)) 989d881ad2c570dcb5965eb00b66462e1075ec82440Chris Lattner GVAlign = std::max(GVAlign, 4U); 990f8a87e8343c5f829be9a75d8160e1bb969b84119Dan Gohman 991d881ad2c570dcb5965eb00b66462e1075ec82440Chris Lattner if (GVAlign > 1) { 992d881ad2c570dcb5965eb00b66462e1075ec82440Chris Lattner unsigned DstWidth = CI2->getType()->getBitWidth(); 9933e5d1d8d0839e98ee203aa3e3c4fe84ef71eba2cChris Lattner unsigned SrcWidth = std::min(DstWidth, Log2_32(GVAlign)); 994d881ad2c570dcb5965eb00b66462e1075ec82440Chris Lattner APInt BitsNotSet(APInt::getLowBitsSet(DstWidth, SrcWidth)); 995d881ad2c570dcb5965eb00b66462e1075ec82440Chris Lattner 996d881ad2c570dcb5965eb00b66462e1075ec82440Chris Lattner // If checking bits we know are clear, return zero. 997d881ad2c570dcb5965eb00b66462e1075ec82440Chris Lattner if ((CI2->getValue() & BitsNotSet) == CI2->getValue()) 998a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson return Constant::getNullValue(CI2->getType()); 999d881ad2c570dcb5965eb00b66462e1075ec82440Chris Lattner } 1000e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer } 10011c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner } 10021c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner break; 10031c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner case Instruction::Or: 100433c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (CI2->equalsInt(0)) return C1; // X | 0 == X 10051c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner if (CI2->isAllOnesValue()) 100633c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky return C2; // X | -1 == -1 10071c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner break; 10081c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner case Instruction::Xor: 100933c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (CI2->equalsInt(0)) return C1; // X ^ 0 == X 10103105ebfaf7105ffe20530a70194fa8d85d07fb7cNick Lewycky 10113105ebfaf7105ffe20530a70194fa8d85d07fb7cNick Lewycky if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) { 10123105ebfaf7105ffe20530a70194fa8d85d07fb7cNick Lewycky switch (CE1->getOpcode()) { 10133105ebfaf7105ffe20530a70194fa8d85d07fb7cNick Lewycky default: break; 10143105ebfaf7105ffe20530a70194fa8d85d07fb7cNick Lewycky case Instruction::ICmp: 10153105ebfaf7105ffe20530a70194fa8d85d07fb7cNick Lewycky case Instruction::FCmp: 1016b13efdafec254746bc83583a10ec3604210d0d35Nick Lewycky // cmp pred ^ true -> cmp !pred 10173105ebfaf7105ffe20530a70194fa8d85d07fb7cNick Lewycky assert(CI2->equalsInt(1)); 10188577e27dd34f190e7014742822b6d2536ebcc796Nick Lewycky CmpInst::Predicate pred = (CmpInst::Predicate)CE1->getPredicate(); 10193105ebfaf7105ffe20530a70194fa8d85d07fb7cNick Lewycky pred = CmpInst::getInversePredicate(pred); 10203105ebfaf7105ffe20530a70194fa8d85d07fb7cNick Lewycky return ConstantExpr::getCompare(pred, CE1->getOperand(0), 10213105ebfaf7105ffe20530a70194fa8d85d07fb7cNick Lewycky CE1->getOperand(1)); 10223105ebfaf7105ffe20530a70194fa8d85d07fb7cNick Lewycky } 10233105ebfaf7105ffe20530a70194fa8d85d07fb7cNick Lewycky } 10241c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner break; 10251c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner case Instruction::AShr: 10261c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner // ashr (zext C to Ty), C2 -> lshr (zext C, CSA), C2 102733c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) 10282ef14d9f7c416105be650021c8010bcf47583065Chris Lattner if (CE1->getOpcode() == Instruction::ZExt) // Top bits known zero. 102933c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky return ConstantExpr::getLShr(C1, C2); 10301c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner break; 1031e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer } 10320e488b3d1c9293bbf2d64d0ecc4d6339f9100351Dan Gohman } else if (isa<ConstantInt>(C1)) { 10330e488b3d1c9293bbf2d64d0ecc4d6339f9100351Dan Gohman // If C1 is a ConstantInt and C2 is not, swap the operands. 10340e488b3d1c9293bbf2d64d0ecc4d6339f9100351Dan Gohman if (Instruction::isCommutative(Opcode)) 10350e488b3d1c9293bbf2d64d0ecc4d6339f9100351Dan Gohman return ConstantExpr::get(Opcode, C2, C1); 10361c14c297460e0a6480bab989c0ade346288bbfd2Chris Lattner } 1037f8a87e8343c5f829be9a75d8160e1bb969b84119Dan Gohman 10380e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner // At this point we know neither constant is an UndefValue. 103933c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (ConstantInt *CI1 = dyn_cast<ConstantInt>(C1)) { 104033c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (ConstantInt *CI2 = dyn_cast<ConstantInt>(C2)) { 10410e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner const APInt &C1V = CI1->getValue(); 10420e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner const APInt &C2V = CI2->getValue(); 1043d333d906734088b074806721ee16aa68359e41c3Chris Lattner switch (Opcode) { 1044d333d906734088b074806721ee16aa68359e41c3Chris Lattner default: 1045d333d906734088b074806721ee16aa68359e41c3Chris Lattner break; 1046d333d906734088b074806721ee16aa68359e41c3Chris Lattner case Instruction::Add: 1047b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(CI1->getContext(), C1V + C2V); 1048d333d906734088b074806721ee16aa68359e41c3Chris Lattner case Instruction::Sub: 1049b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(CI1->getContext(), C1V - C2V); 1050d333d906734088b074806721ee16aa68359e41c3Chris Lattner case Instruction::Mul: 1051b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(CI1->getContext(), C1V * C2V); 1052d333d906734088b074806721ee16aa68359e41c3Chris Lattner case Instruction::UDiv: 1053c9a005807ab46d03a3b24f84220542c6eb84e9d1Chris Lattner assert(!CI2->isNullValue() && "Div by zero handled above"); 1054b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(CI1->getContext(), C1V.udiv(C2V)); 1055d333d906734088b074806721ee16aa68359e41c3Chris Lattner case Instruction::SDiv: 1056c9a005807ab46d03a3b24f84220542c6eb84e9d1Chris Lattner assert(!CI2->isNullValue() && "Div by zero handled above"); 10579472c373a3378982ab4ef1a2caafabd4acf58ba2Reid Spencer if (C2V.isAllOnesValue() && C1V.isMinSignedValue()) 10589e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson return UndefValue::get(CI1->getType()); // MIN_INT / -1 -> undef 1059b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(CI1->getContext(), C1V.sdiv(C2V)); 10609472c373a3378982ab4ef1a2caafabd4acf58ba2Reid Spencer case Instruction::URem: 1061c9a005807ab46d03a3b24f84220542c6eb84e9d1Chris Lattner assert(!CI2->isNullValue() && "Div by zero handled above"); 1062b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(CI1->getContext(), C1V.urem(C2V)); 1063c9a005807ab46d03a3b24f84220542c6eb84e9d1Chris Lattner case Instruction::SRem: 1064c9a005807ab46d03a3b24f84220542c6eb84e9d1Chris Lattner assert(!CI2->isNullValue() && "Div by zero handled above"); 10659472c373a3378982ab4ef1a2caafabd4acf58ba2Reid Spencer if (C2V.isAllOnesValue() && C1V.isMinSignedValue()) 10669e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson return UndefValue::get(CI1->getType()); // MIN_INT % -1 -> undef 1067b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(CI1->getContext(), C1V.srem(C2V)); 1068d333d906734088b074806721ee16aa68359e41c3Chris Lattner case Instruction::And: 1069b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(CI1->getContext(), C1V & C2V); 1070d333d906734088b074806721ee16aa68359e41c3Chris Lattner case Instruction::Or: 1071b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(CI1->getContext(), C1V | C2V); 1072d333d906734088b074806721ee16aa68359e41c3Chris Lattner case Instruction::Xor: 1073b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(CI1->getContext(), C1V ^ C2V); 10740e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner case Instruction::Shl: { 10750e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner uint32_t shiftAmt = C2V.getZExtValue(); 10760e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner if (shiftAmt < C1V.getBitWidth()) 1077b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(CI1->getContext(), C1V.shl(shiftAmt)); 10780e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner else 10799e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson return UndefValue::get(C1->getType()); // too big shift is undef 10800e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner } 10810e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner case Instruction::LShr: { 10820e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner uint32_t shiftAmt = C2V.getZExtValue(); 10830e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner if (shiftAmt < C1V.getBitWidth()) 1084b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(CI1->getContext(), C1V.lshr(shiftAmt)); 10850e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner else 10869e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson return UndefValue::get(C1->getType()); // too big shift is undef 10870e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner } 10880e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner case Instruction::AShr: { 10890e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner uint32_t shiftAmt = C2V.getZExtValue(); 10900e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner if (shiftAmt < C1V.getBitWidth()) 1091b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(CI1->getContext(), C1V.ashr(shiftAmt)); 10920e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner else 10939e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson return UndefValue::get(C1->getType()); // too big shift is undef 10940e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner } 1095e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer } 1096e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer } 1097e47f59db448fd468bb32ade37a1fa7ee8a9bff20Nick Lewycky 1098e47f59db448fd468bb32ade37a1fa7ee8a9bff20Nick Lewycky switch (Opcode) { 1099e47f59db448fd468bb32ade37a1fa7ee8a9bff20Nick Lewycky case Instruction::SDiv: 1100e47f59db448fd468bb32ade37a1fa7ee8a9bff20Nick Lewycky case Instruction::UDiv: 1101e47f59db448fd468bb32ade37a1fa7ee8a9bff20Nick Lewycky case Instruction::URem: 1102e47f59db448fd468bb32ade37a1fa7ee8a9bff20Nick Lewycky case Instruction::SRem: 1103e47f59db448fd468bb32ade37a1fa7ee8a9bff20Nick Lewycky case Instruction::LShr: 1104e47f59db448fd468bb32ade37a1fa7ee8a9bff20Nick Lewycky case Instruction::AShr: 1105e47f59db448fd468bb32ade37a1fa7ee8a9bff20Nick Lewycky case Instruction::Shl: 110633c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (CI1->equalsInt(0)) return C1; 1107e47f59db448fd468bb32ade37a1fa7ee8a9bff20Nick Lewycky break; 1108e47f59db448fd468bb32ade37a1fa7ee8a9bff20Nick Lewycky default: 1109e47f59db448fd468bb32ade37a1fa7ee8a9bff20Nick Lewycky break; 1110e47f59db448fd468bb32ade37a1fa7ee8a9bff20Nick Lewycky } 111133c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky } else if (ConstantFP *CFP1 = dyn_cast<ConstantFP>(C1)) { 111233c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (ConstantFP *CFP2 = dyn_cast<ConstantFP>(C2)) { 111343421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen APFloat C1V = CFP1->getValueAPF(); 111443421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen APFloat C2V = CFP2->getValueAPF(); 111543421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen APFloat C3V = C1V; // copy for modification 1116e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer switch (Opcode) { 1117e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer default: 1118e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer break; 1119ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman case Instruction::FAdd: 112043421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen (void)C3V.add(C2V, APFloat::rmNearestTiesToEven); 1121b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantFP::get(C1->getContext(), C3V); 1122ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman case Instruction::FSub: 112343421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen (void)C3V.subtract(C2V, APFloat::rmNearestTiesToEven); 1124b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantFP::get(C1->getContext(), C3V); 1125ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman case Instruction::FMul: 112643421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen (void)C3V.multiply(C2V, APFloat::rmNearestTiesToEven); 1127b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantFP::get(C1->getContext(), C3V); 1128e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case Instruction::FDiv: 112943421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen (void)C3V.divide(C2V, APFloat::rmNearestTiesToEven); 1130b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantFP::get(C1->getContext(), C3V); 1131e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case Instruction::FRem: 113243421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen (void)C3V.mod(C2V, APFloat::rmNearestTiesToEven); 1133b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantFP::get(C1->getContext(), C3V); 1134e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer } 1135e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer } 1136db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner } else if (VectorType *VTy = dyn_cast<VectorType>(C1->getType())) { 1137d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner // Perform elementwise folding. 1138d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner SmallVector<Constant*, 16> Result; 113903e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman Type *Ty = IntegerType::get(VTy->getContext(), 32); 1140d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) { 114103e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman Constant *LHS = 114203e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman ConstantExpr::getExtractElement(C1, ConstantInt::get(Ty, i)); 114303e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman Constant *RHS = 114403e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman ConstantExpr::getExtractElement(C2, ConstantInt::get(Ty, i)); 1145d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner 1146d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner Result.push_back(ConstantExpr::get(Opcode, LHS, RHS)); 1147e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer } 1148d59ae907eea28285ece6696d6f3271b4ca578c0dChris Lattner 114903e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman return ConstantVector::get(Result); 1150e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer } 1151e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer 11524f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) { 11530e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner // There are many possible foldings we could do here. We should probably 11540e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner // at least fold add of a pointer with an integer into the appropriate 11550e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner // getelementptr. This will improve alias analysis a bit. 11564f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman 11574f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman // Given ((a + b) + c), if (b + c) folds to something interesting, return 11584f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman // (a + (b + c)). 11590d7ce5ffa4aa853b75e1015c62e27bd9f23ef73bDuncan Sands if (Instruction::isAssociative(Opcode) && CE1->getOpcode() == Opcode) { 11604f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman Constant *T = ConstantExpr::get(Opcode, CE1->getOperand(1), C2); 11614f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman if (!isa<ConstantExpr>(T) || cast<ConstantExpr>(T)->getOpcode() != Opcode) 11624f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman return ConstantExpr::get(Opcode, CE1->getOperand(0), T); 11634f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman } 11640e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner } else if (isa<ConstantExpr>(C2)) { 11650e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner // If C2 is a constant expr and C1 isn't, flop them around and fold the 11660e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner // other way if possible. 1167d97439d1e5a04a580c48b98512885866dcc83e2aDan Gohman if (Instruction::isCommutative(Opcode)) 1168b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantFoldBinaryInstruction(Opcode, C2, C1); 11690e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner } 1170f8a87e8343c5f829be9a75d8160e1bb969b84119Dan Gohman 1171f4d18827430be9ac3a7a5216be61224146d97f44Nick Lewycky // i1 can be simplified in many cases. 1172b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands if (C1->getType()->isIntegerTy(1)) { 1173f4d18827430be9ac3a7a5216be61224146d97f44Nick Lewycky switch (Opcode) { 1174f4d18827430be9ac3a7a5216be61224146d97f44Nick Lewycky case Instruction::Add: 1175f4d18827430be9ac3a7a5216be61224146d97f44Nick Lewycky case Instruction::Sub: 117633c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky return ConstantExpr::getXor(C1, C2); 1177f4d18827430be9ac3a7a5216be61224146d97f44Nick Lewycky case Instruction::Mul: 117833c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky return ConstantExpr::getAnd(C1, C2); 1179f4d18827430be9ac3a7a5216be61224146d97f44Nick Lewycky case Instruction::Shl: 1180f4d18827430be9ac3a7a5216be61224146d97f44Nick Lewycky case Instruction::LShr: 1181f4d18827430be9ac3a7a5216be61224146d97f44Nick Lewycky case Instruction::AShr: 1182f4d18827430be9ac3a7a5216be61224146d97f44Nick Lewycky // We can assume that C2 == 0. If it were one the result would be 1183f4d18827430be9ac3a7a5216be61224146d97f44Nick Lewycky // undefined because the shift value is as large as the bitwidth. 118433c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky return C1; 1185f4d18827430be9ac3a7a5216be61224146d97f44Nick Lewycky case Instruction::SDiv: 1186f4d18827430be9ac3a7a5216be61224146d97f44Nick Lewycky case Instruction::UDiv: 1187f4d18827430be9ac3a7a5216be61224146d97f44Nick Lewycky // We can assume that C2 == 1. If it were zero the result would be 1188f4d18827430be9ac3a7a5216be61224146d97f44Nick Lewycky // undefined through division by zero. 118933c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky return C1; 1190f4d18827430be9ac3a7a5216be61224146d97f44Nick Lewycky case Instruction::URem: 1191f4d18827430be9ac3a7a5216be61224146d97f44Nick Lewycky case Instruction::SRem: 1192f4d18827430be9ac3a7a5216be61224146d97f44Nick Lewycky // We can assume that C2 == 1. If it were zero the result would be 1193f4d18827430be9ac3a7a5216be61224146d97f44Nick Lewycky // undefined through division by zero. 1194b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::getFalse(C1->getContext()); 1195f4d18827430be9ac3a7a5216be61224146d97f44Nick Lewycky default: 1196f4d18827430be9ac3a7a5216be61224146d97f44Nick Lewycky break; 1197f4d18827430be9ac3a7a5216be61224146d97f44Nick Lewycky } 1198f4d18827430be9ac3a7a5216be61224146d97f44Nick Lewycky } 1199f4d18827430be9ac3a7a5216be61224146d97f44Nick Lewycky 12000e4b6c7551776a801fa3caf9e05f47b5c8e001dcChris Lattner // We don't know how to fold this. 1201e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return 0; 1202e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer} 120300f1023cf8b30c74dc219525f518a80c45b6e7baChris Lattner 1204ce04a6d7f6ce25c7d765a57baa30a70993a46021Chris Lattner/// isZeroSizedType - This type is zero sized if its an array or structure of 1205ce04a6d7f6ce25c7d765a57baa30a70993a46021Chris Lattner/// zero sized types. The only leaf zero sized type is an empty structure. 1206db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattnerstatic bool isMaybeZeroSizedType(Type *Ty) { 1207db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner if (StructType *STy = dyn_cast<StructType>(Ty)) { 12081afcace3a3a138b1b18e5c6270caa8dae2261ae2Chris Lattner if (STy->isOpaque()) return true; // Can't say. 1209ce04a6d7f6ce25c7d765a57baa30a70993a46021Chris Lattner 1210ce04a6d7f6ce25c7d765a57baa30a70993a46021Chris Lattner // If all of elements have zero size, this does too. 1211ce04a6d7f6ce25c7d765a57baa30a70993a46021Chris Lattner for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) 1212f4aa3351de8c997cef7a33e6946c6083e425618bChris Lattner if (!isMaybeZeroSizedType(STy->getElementType(i))) return false; 1213ce04a6d7f6ce25c7d765a57baa30a70993a46021Chris Lattner return true; 1214ce04a6d7f6ce25c7d765a57baa30a70993a46021Chris Lattner 1215db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { 1216ce04a6d7f6ce25c7d765a57baa30a70993a46021Chris Lattner return isMaybeZeroSizedType(ATy->getElementType()); 1217ce04a6d7f6ce25c7d765a57baa30a70993a46021Chris Lattner } 1218ce04a6d7f6ce25c7d765a57baa30a70993a46021Chris Lattner return false; 1219ce04a6d7f6ce25c7d765a57baa30a70993a46021Chris Lattner} 1220e97148628f8a52fff1752937f6f49945bbe9937bChris Lattner 1221504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner/// IdxCompare - Compare the two constants as though they were getelementptr 1222504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner/// indices. This allows coersion of the types to be the same thing. 1223504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner/// 1224504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner/// If the two constants are the "same" (after coersion), return 0. If the 1225504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner/// first is less than the second, return -1, if the second is less than the 1226504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner/// first, return 1. If the constants are not integral, return -2. 1227504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner/// 1228db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattnerstatic int IdxCompare(Constant *C1, Constant *C2, Type *ElTy) { 1229504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner if (C1 == C2) return 0; 1230504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner 1231cc615c38b189f36b4a4a2b515232f69e63717d26Reid Spencer // Ok, we found a different index. If they are not ConstantInt, we can't do 1232cc615c38b189f36b4a4a2b515232f69e63717d26Reid Spencer // anything with them. 1233504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner if (!isa<ConstantInt>(C1) || !isa<ConstantInt>(C2)) 1234504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner return -2; // don't know! 1235fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 123628977af72a11fcad5d1b54d7a96b3df02828f6fcChris Lattner // Ok, we have two differing integer indices. Sign extend them to be the same 123728977af72a11fcad5d1b54d7a96b3df02828f6fcChris Lattner // type. Long is always big enough, so we use it. 1238b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands if (!C1->getType()->isIntegerTy(64)) 1239b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner C1 = ConstantExpr::getSExt(C1, Type::getInt64Ty(C1->getContext())); 124079e21d338c60b4b5a5746fc45e37ea0310606aeeReid Spencer 1241b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands if (!C2->getType()->isIntegerTy(64)) 1242b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner C2 = ConstantExpr::getSExt(C2, Type::getInt64Ty(C1->getContext())); 12433da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer 124479e21d338c60b4b5a5746fc45e37ea0310606aeeReid Spencer if (C1 == C2) return 0; // They are equal 1245504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner 1246ce04a6d7f6ce25c7d765a57baa30a70993a46021Chris Lattner // If the type being indexed over is really just a zero sized type, there is 1247ce04a6d7f6ce25c7d765a57baa30a70993a46021Chris Lattner // no pointer difference being made here. 1248ce04a6d7f6ce25c7d765a57baa30a70993a46021Chris Lattner if (isMaybeZeroSizedType(ElTy)) 1249ce04a6d7f6ce25c7d765a57baa30a70993a46021Chris Lattner return -2; // dunno. 1250ce04a6d7f6ce25c7d765a57baa30a70993a46021Chris Lattner 1251504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // If they are really different, now that they are the same type, then we 1252504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // found a difference! 1253b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer if (cast<ConstantInt>(C1)->getSExtValue() < 1254b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer cast<ConstantInt>(C2)->getSExtValue()) 1255504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner return -1; 1256504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner else 1257504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner return 1; 1258504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner} 1259504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner 1260898b2d52f9802fb8d9c426fa1c7e28c91cf2b1d8Chris Lattner/// evaluateFCmpRelation - This function determines if there is anything we can 1261e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer/// decide about the two constants provided. This doesn't need to handle simple 1262e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer/// things like ConstantFP comparisons, but should instead handle ConstantExprs. 1263e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer/// If we can determine that the two constants have a particular relation to 1264e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer/// each other, we should return the corresponding FCmpInst predicate, 1265b913bbaa415299b732d72239566e203fe57761d6Reid Spencer/// otherwise return FCmpInst::BAD_FCMP_PREDICATE. This is used below in 1266b913bbaa415299b732d72239566e203fe57761d6Reid Spencer/// ConstantFoldCompareInstruction. 1267e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer/// 1268e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer/// To simplify this code we canonicalize the relation so that the first 1269b913bbaa415299b732d72239566e203fe57761d6Reid Spencer/// operand is always the most "complex" of the two. We consider ConstantFP 1270b913bbaa415299b732d72239566e203fe57761d6Reid Spencer/// to be the simplest, and ConstantExprs to be the most complex. 1271b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattnerstatic FCmpInst::Predicate evaluateFCmpRelation(Constant *V1, Constant *V2) { 1272e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer assert(V1->getType() == V2->getType() && 1273b913bbaa415299b732d72239566e203fe57761d6Reid Spencer "Cannot compare values of different types!"); 12745927d8e94d0b50296c2af4acdfe4d799e369fc08Dale Johannesen 1275b913bbaa415299b732d72239566e203fe57761d6Reid Spencer // Handle degenerate case quickly 1276e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer if (V1 == V2) return FCmpInst::FCMP_OEQ; 1277e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer 1278b913bbaa415299b732d72239566e203fe57761d6Reid Spencer if (!isa<ConstantExpr>(V1)) { 1279b913bbaa415299b732d72239566e203fe57761d6Reid Spencer if (!isa<ConstantExpr>(V2)) { 1280b913bbaa415299b732d72239566e203fe57761d6Reid Spencer // We distilled thisUse the standard constant folder for a few cases 12816b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng ConstantInt *R = 0; 12826b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng R = dyn_cast<ConstantInt>( 128333c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, V1, V2)); 1284cae5754619433aed7be74abbf1c0551a82d369cbReid Spencer if (R && !R->isZero()) 1285e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return FCmpInst::FCMP_OEQ; 12866b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng R = dyn_cast<ConstantInt>( 128733c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky ConstantExpr::getFCmp(FCmpInst::FCMP_OLT, V1, V2)); 1288cae5754619433aed7be74abbf1c0551a82d369cbReid Spencer if (R && !R->isZero()) 1289e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return FCmpInst::FCMP_OLT; 12906b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng R = dyn_cast<ConstantInt>( 129133c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky ConstantExpr::getFCmp(FCmpInst::FCMP_OGT, V1, V2)); 1292cae5754619433aed7be74abbf1c0551a82d369cbReid Spencer if (R && !R->isZero()) 1293b913bbaa415299b732d72239566e203fe57761d6Reid Spencer return FCmpInst::FCMP_OGT; 1294b913bbaa415299b732d72239566e203fe57761d6Reid Spencer 1295b913bbaa415299b732d72239566e203fe57761d6Reid Spencer // Nothing more we can do 1296e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return FCmpInst::BAD_FCMP_PREDICATE; 1297e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer } 1298f8a87e8343c5f829be9a75d8160e1bb969b84119Dan Gohman 1299b913bbaa415299b732d72239566e203fe57761d6Reid Spencer // If the first operand is simple and second is ConstantExpr, swap operands. 1300b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner FCmpInst::Predicate SwappedRelation = evaluateFCmpRelation(V2, V1); 1301b913bbaa415299b732d72239566e203fe57761d6Reid Spencer if (SwappedRelation != FCmpInst::BAD_FCMP_PREDICATE) 1302b913bbaa415299b732d72239566e203fe57761d6Reid Spencer return FCmpInst::getSwappedPredicate(SwappedRelation); 1303b913bbaa415299b732d72239566e203fe57761d6Reid Spencer } else { 1304b913bbaa415299b732d72239566e203fe57761d6Reid Spencer // Ok, the LHS is known to be a constantexpr. The RHS can be any of a 1305b913bbaa415299b732d72239566e203fe57761d6Reid Spencer // constantexpr or a simple constant. 130633c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky ConstantExpr *CE1 = cast<ConstantExpr>(V1); 1307b913bbaa415299b732d72239566e203fe57761d6Reid Spencer switch (CE1->getOpcode()) { 1308b913bbaa415299b732d72239566e203fe57761d6Reid Spencer case Instruction::FPTrunc: 1309b913bbaa415299b732d72239566e203fe57761d6Reid Spencer case Instruction::FPExt: 1310b913bbaa415299b732d72239566e203fe57761d6Reid Spencer case Instruction::UIToFP: 1311b913bbaa415299b732d72239566e203fe57761d6Reid Spencer case Instruction::SIToFP: 1312b913bbaa415299b732d72239566e203fe57761d6Reid Spencer // We might be able to do something with these but we don't right now. 1313b913bbaa415299b732d72239566e203fe57761d6Reid Spencer break; 1314b913bbaa415299b732d72239566e203fe57761d6Reid Spencer default: 1315b913bbaa415299b732d72239566e203fe57761d6Reid Spencer break; 1316b913bbaa415299b732d72239566e203fe57761d6Reid Spencer } 1317e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer } 1318e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer // There are MANY other foldings that we could perform here. They will 1319e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer // probably be added on demand, as they seem needed. 1320e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return FCmpInst::BAD_FCMP_PREDICATE; 1321e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer} 1322e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer 1323e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer/// evaluateICmpRelation - This function determines if there is anything we can 1324504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner/// decide about the two constants provided. This doesn't need to handle simple 13257970396014eacbe719eb171448ddc546c1ad2289Reid Spencer/// things like integer comparisons, but should instead handle ConstantExprs 132636c2451ddbe80dc9644b91fe1d31e7fb82b11e13Chris Lattner/// and GlobalValues. If we can determine that the two constants have a 1327e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer/// particular relation to each other, we should return the corresponding ICmp 1328e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer/// predicate, otherwise return ICmpInst::BAD_ICMP_PREDICATE. 1329504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner/// 1330504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner/// To simplify this code we canonicalize the relation so that the first 1331504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner/// operand is always the most "complex" of the two. We consider simple 1332504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner/// constants (like ConstantInt) to be the simplest, followed by 13337970396014eacbe719eb171448ddc546c1ad2289Reid Spencer/// GlobalValues, followed by ConstantExpr's (the most complex). 1334504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner/// 1335b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattnerstatic ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2, 1336e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer bool isSigned) { 1337504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner assert(V1->getType() == V2->getType() && 1338504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner "Cannot compare different types of values!"); 1339e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer if (V1 == V2) return ICmpInst::ICMP_EQ; 1340504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner 1341b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner if (!isa<ConstantExpr>(V1) && !isa<GlobalValue>(V1) && 1342b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner !isa<BlockAddress>(V1)) { 1343b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner if (!isa<GlobalValue>(V2) && !isa<ConstantExpr>(V2) && 1344b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner !isa<BlockAddress>(V2)) { 13452f690c84fa1682a2009423539af16d5f530006f9Chris Lattner // We distilled this down to a simple case, use the standard constant 13462f690c84fa1682a2009423539af16d5f530006f9Chris Lattner // folder. 13476b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng ConstantInt *R = 0; 1348e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer ICmpInst::Predicate pred = ICmpInst::ICMP_EQ; 134933c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2)); 1350cae5754619433aed7be74abbf1c0551a82d369cbReid Spencer if (R && !R->isZero()) 1351e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return pred; 1352e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer pred = isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; 135333c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2)); 1354cae5754619433aed7be74abbf1c0551a82d369cbReid Spencer if (R && !R->isZero()) 1355e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return pred; 13567f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky pred = isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; 135733c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky R = dyn_cast<ConstantInt>(ConstantExpr::getICmp(pred, V1, V2)); 1358cae5754619433aed7be74abbf1c0551a82d369cbReid Spencer if (R && !R->isZero()) 1359e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return pred; 1360f8a87e8343c5f829be9a75d8160e1bb969b84119Dan Gohman 13612f690c84fa1682a2009423539af16d5f530006f9Chris Lattner // If we couldn't figure it out, bail. 1362e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return ICmpInst::BAD_ICMP_PREDICATE; 13632f690c84fa1682a2009423539af16d5f530006f9Chris Lattner } 1364f8a87e8343c5f829be9a75d8160e1bb969b84119Dan Gohman 1365504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // If the first operand is simple, swap operands. 1366e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer ICmpInst::Predicate SwappedRelation = 1367b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner evaluateICmpRelation(V2, V1, isSigned); 1368e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE) 1369e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return ICmpInst::getSwappedPredicate(SwappedRelation); 1370504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner 1371b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) { 1372b97e2788ff303e83eab8d24960966bfde05e6a37Chris Lattner if (isa<ConstantExpr>(V2)) { // Swap as necessary. 1373e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer ICmpInst::Predicate SwappedRelation = 1374b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner evaluateICmpRelation(V2, V1, isSigned); 1375e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE) 1376e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return ICmpInst::getSwappedPredicate(SwappedRelation); 1377b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner return ICmpInst::BAD_ICMP_PREDICATE; 1378b97e2788ff303e83eab8d24960966bfde05e6a37Chris Lattner } 1379504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner 1380b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner // Now we know that the RHS is a GlobalValue, BlockAddress or simple 1381b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner // constant (which, since the types must match, means that it's a 1382b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner // ConstantPointerNull). 1383b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) { 1384a0ef5ed742a81b134ac4438c3a4adc0c9a151b64Chris Lattner // Don't try to decide equality of aliases. 1385b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner if (!isa<GlobalAlias>(GV) && !isa<GlobalAlias>(GV2)) 1386b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner if (!GV->hasExternalWeakLinkage() || !GV2->hasExternalWeakLinkage()) 1387a0ef5ed742a81b134ac4438c3a4adc0c9a151b64Chris Lattner return ICmpInst::ICMP_NE; 1388b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner } else if (isa<BlockAddress>(V2)) { 1389b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner return ICmpInst::ICMP_NE; // Globals never equal labels. 1390504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner } else { 1391504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!"); 1392b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner // GlobalVals can never be null unless they have external weak linkage. 1393b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner // We don't try to evaluate aliases here. 1394b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner if (!GV->hasExternalWeakLinkage() && !isa<GlobalAlias>(GV)) 1395e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return ICmpInst::ICMP_NE; 1396504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner } 1397b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) { 1398b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner if (isa<ConstantExpr>(V2)) { // Swap as necessary. 1399b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner ICmpInst::Predicate SwappedRelation = 1400b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner evaluateICmpRelation(V2, V1, isSigned); 1401b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner if (SwappedRelation != ICmpInst::BAD_ICMP_PREDICATE) 1402b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner return ICmpInst::getSwappedPredicate(SwappedRelation); 1403b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner return ICmpInst::BAD_ICMP_PREDICATE; 1404b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner } 1405b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner 1406b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner // Now we know that the RHS is a GlobalValue, BlockAddress or simple 1407b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner // constant (which, since the types must match, means that it is a 1408b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner // ConstantPointerNull). 1409b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner if (const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) { 1410b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner // Block address in another function can't equal this one, but block 1411b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner // addresses in the current function might be the same if blocks are 1412b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner // empty. 1413b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner if (BA2->getFunction() != BA->getFunction()) 1414b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner return ICmpInst::ICMP_NE; 1415b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner } else { 1416b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner // Block addresses aren't null, don't equal the address of globals. 1417b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner assert((isa<ConstantPointerNull>(V2) || isa<GlobalValue>(V2)) && 1418b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner "Canonicalization guarantee!"); 1419b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner return ICmpInst::ICMP_NE; 1420b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner } 1421504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner } else { 1422504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // Ok, the LHS is known to be a constantexpr. The RHS can be any of a 1423b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner // constantexpr, a global, block address, or a simple constant. 142433c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky ConstantExpr *CE1 = cast<ConstantExpr>(V1); 142533c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky Constant *CE1Op0 = CE1->getOperand(0); 1426504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner 1427504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner switch (CE1->getOpcode()) { 14283da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer case Instruction::Trunc: 14293da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer case Instruction::FPTrunc: 14303da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer case Instruction::FPExt: 14313da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer case Instruction::FPToUI: 14323da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer case Instruction::FPToSI: 1433e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer break; // We can't evaluate floating point casts or truncations. 1434e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer 14353da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer case Instruction::UIToFP: 14363da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer case Instruction::SIToFP: 14373da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer case Instruction::BitCast: 1438e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case Instruction::ZExt: 1439e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case Instruction::SExt: 1440504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // If the cast is not actually changing bits, and the second operand is a 1441504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // null pointer, do the comparison with the pre-casted value. 1442504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner if (V2->isNullValue() && 14431df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands (CE1->getType()->isPointerTy() || CE1->getType()->isIntegerTy())) { 1444f286f6fd93d569befe6e77c94a947e6e04e95685Chris Lattner if (CE1->getOpcode() == Instruction::ZExt) isSigned = false; 1445f286f6fd93d569befe6e77c94a947e6e04e95685Chris Lattner if (CE1->getOpcode() == Instruction::SExt) isSigned = true; 1446b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return evaluateICmpRelation(CE1Op0, 1447a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson Constant::getNullValue(CE1Op0->getType()), 1448d43737bd0935150de2e3f385ad8418f2f3a7d2a2Nick Lewycky isSigned); 1449e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer } 1450a0ae8196a1d2d1291949e268afc1bf932c9f8028Chris Lattner break; 1451504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner 1452504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner case Instruction::GetElementPtr: 1453504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // Ok, since this is a getelementptr, we know that the constant has a 1454504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // pointer type. Check the various cases. 1455504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner if (isa<ConstantPointerNull>(V2)) { 1456504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // If we are comparing a GEP to a null pointer, check to see if the base 1457504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // of the GEP equals the null pointer. 1458b913bbaa415299b732d72239566e203fe57761d6Reid Spencer if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) { 145987d5f6c29f098d464f6881e6652aab13a3bb70dbReid Spencer if (GV->hasExternalWeakLinkage()) 146087d5f6c29f098d464f6881e6652aab13a3bb70dbReid Spencer // Weak linkage GVals could be zero or not. We're comparing that 146187d5f6c29f098d464f6881e6652aab13a3bb70dbReid Spencer // to null pointer so its greater-or-equal 1462e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return isSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; 146387d5f6c29f098d464f6881e6652aab13a3bb70dbReid Spencer else 146487d5f6c29f098d464f6881e6652aab13a3bb70dbReid Spencer // If its not weak linkage, the GVal must have a non-zero address 146587d5f6c29f098d464f6881e6652aab13a3bb70dbReid Spencer // so the result is greater-than 14663892baac42daadf760d140168720be1d6e585aa8Nick Lewycky return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; 1467504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner } else if (isa<ConstantPointerNull>(CE1Op0)) { 1468504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // If we are indexing from a null pointer, check to see if we have any 1469504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // non-zero indices. 1470504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner for (unsigned i = 1, e = CE1->getNumOperands(); i != e; ++i) 1471504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner if (!CE1->getOperand(i)->isNullValue()) 1472504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // Offsetting from null, must not be equal. 1473e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; 1474504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // Only zero indexes from null, must still be zero. 1475e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return ICmpInst::ICMP_EQ; 1476504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner } 1477504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // Otherwise, we can't really say if the first operand is null or not. 1478b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner } else if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) { 1479504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner if (isa<ConstantPointerNull>(CE1Op0)) { 1480b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner if (GV2->hasExternalWeakLinkage()) 148187d5f6c29f098d464f6881e6652aab13a3bb70dbReid Spencer // Weak linkage GVals could be zero or not. We're comparing it to 148287d5f6c29f098d464f6881e6652aab13a3bb70dbReid Spencer // a null pointer, so its less-or-equal 1483e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return isSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; 148487d5f6c29f098d464f6881e6652aab13a3bb70dbReid Spencer else 148587d5f6c29f098d464f6881e6652aab13a3bb70dbReid Spencer // If its not weak linkage, the GVal must have a non-zero address 148687d5f6c29f098d464f6881e6652aab13a3bb70dbReid Spencer // so the result is less-than 1487e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; 1488b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CE1Op0)) { 1489b63127d4359d6a51c35f4ebfabfceac8bbd8364aChris Lattner if (GV == GV2) { 1490504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // If this is a getelementptr of the same global, then it must be 1491504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // different. Because the types must match, the getelementptr could 1492504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // only have at most one index, and because we fold getelementptr's 1493504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // with a single zero index, it must be nonzero. 1494504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner assert(CE1->getNumOperands() == 2 && 1495504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner !CE1->getOperand(1)->isNullValue() && 14967a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner "Surprising getelementptr!"); 1497e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; 1498504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner } else { 1499d363ae52995059906d99f7588f47bf891e7db485Dan Gohman // If they are different globals, we don't know what the value is. 1500d363ae52995059906d99f7588f47bf891e7db485Dan Gohman return ICmpInst::BAD_ICMP_PREDICATE; 1501504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner } 1502504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner } 1503504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner } else { 150433c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky ConstantExpr *CE2 = cast<ConstantExpr>(V2); 150533c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky Constant *CE2Op0 = CE2->getOperand(0); 1506504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner 1507504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // There are MANY other foldings that we could perform here. They will 1508504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // probably be added on demand, as they seem needed. 1509504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner switch (CE2->getOpcode()) { 1510504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner default: break; 1511504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner case Instruction::GetElementPtr: 1512504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // By far the most common case to handle is when the base pointers are 1513d363ae52995059906d99f7588f47bf891e7db485Dan Gohman // obviously to the same global. 15147970396014eacbe719eb171448ddc546c1ad2289Reid Spencer if (isa<GlobalValue>(CE1Op0) && isa<GlobalValue>(CE2Op0)) { 1515d363ae52995059906d99f7588f47bf891e7db485Dan Gohman if (CE1Op0 != CE2Op0) // Don't know relative ordering. 1516d363ae52995059906d99f7588f47bf891e7db485Dan Gohman return ICmpInst::BAD_ICMP_PREDICATE; 1517504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // Ok, we know that both getelementptr instructions are based on the 1518504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // same global. From this, we can precisely determine the relative 1519504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // ordering of the resultant pointers. 1520504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner unsigned i = 1; 1521fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 1522e6992f728a94654e43269580a10a667f18dadba9Dan Gohman // The logic below assumes that the result of the comparison 1523e6992f728a94654e43269580a10a667f18dadba9Dan Gohman // can be determined by finding the first index that differs. 1524e6992f728a94654e43269580a10a667f18dadba9Dan Gohman // This doesn't work if there is over-indexing in any 1525e6992f728a94654e43269580a10a667f18dadba9Dan Gohman // subsequent indices, so check for that case first. 1526e6992f728a94654e43269580a10a667f18dadba9Dan Gohman if (!CE1->isGEPWithNoNotionalOverIndexing() || 1527e6992f728a94654e43269580a10a667f18dadba9Dan Gohman !CE2->isGEPWithNoNotionalOverIndexing()) 1528e6992f728a94654e43269580a10a667f18dadba9Dan Gohman return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal. 1529e6992f728a94654e43269580a10a667f18dadba9Dan Gohman 1530504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // Compare all of the operands the GEP's have in common. 1531ce04a6d7f6ce25c7d765a57baa30a70993a46021Chris Lattner gep_type_iterator GTI = gep_type_begin(CE1); 1532ce04a6d7f6ce25c7d765a57baa30a70993a46021Chris Lattner for (;i != CE1->getNumOperands() && i != CE2->getNumOperands(); 1533ce04a6d7f6ce25c7d765a57baa30a70993a46021Chris Lattner ++i, ++GTI) 1534b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner switch (IdxCompare(CE1->getOperand(i), 15350a5372ed3e8cda10d724feda3c1a1c998db05ca0Owen Anderson CE2->getOperand(i), GTI.getIndexedType())) { 1536e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case -1: return isSigned ? ICmpInst::ICMP_SLT:ICmpInst::ICMP_ULT; 1537e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case 1: return isSigned ? ICmpInst::ICMP_SGT:ICmpInst::ICMP_UGT; 1538e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case -2: return ICmpInst::BAD_ICMP_PREDICATE; 1539504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner } 1540504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner 1541504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // Ok, we ran out of things they have in common. If any leftovers 1542504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner // are non-zero then we have a difference, otherwise we are equal. 1543504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner for (; i < CE1->getNumOperands(); ++i) 1544ae9f3a3b7c915f725aef5a7250e88eaeddda03c6Anton Korobeynikov if (!CE1->getOperand(i)->isNullValue()) { 15456b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng if (isa<ConstantInt>(CE1->getOperand(i))) 1546e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return isSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; 1547ce04a6d7f6ce25c7d765a57baa30a70993a46021Chris Lattner else 1548e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal. 1549ae9f3a3b7c915f725aef5a7250e88eaeddda03c6Anton Korobeynikov } 1550fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 1551504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner for (; i < CE2->getNumOperands(); ++i) 1552ae9f3a3b7c915f725aef5a7250e88eaeddda03c6Anton Korobeynikov if (!CE2->getOperand(i)->isNullValue()) { 15536b6b6ef1677fa71b1072c2911b4c1f9524a558c9Zhou Sheng if (isa<ConstantInt>(CE2->getOperand(i))) 1554e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; 1555ce04a6d7f6ce25c7d765a57baa30a70993a46021Chris Lattner else 1556e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return ICmpInst::BAD_ICMP_PREDICATE; // Might be equal. 1557ae9f3a3b7c915f725aef5a7250e88eaeddda03c6Anton Korobeynikov } 1558e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return ICmpInst::ICMP_EQ; 1559504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner } 1560504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner } 1561504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner } 1562504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner default: 1563504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner break; 1564504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner } 1565504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner } 1566504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner 1567e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer return ICmpInst::BAD_ICMP_PREDICATE; 1568504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner} 1569504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner 1570b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris LattnerConstant *llvm::ConstantFoldCompareInstruction(unsigned short pred, 157133c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky Constant *C1, Constant *C2) { 1572db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *ResultTy; 1573db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner if (VectorType *VT = dyn_cast<VectorType>(C1->getType())) 1574b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner ResultTy = VectorType::get(Type::getInt1Ty(C1->getContext()), 1575b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner VT->getNumElements()); 15767f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky else 1577b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner ResultTy = Type::getInt1Ty(C1->getContext()); 15787f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky 1579155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner // Fold FCMP_FALSE/FCMP_TRUE unconditionally. 15807f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky if (pred == FCmpInst::FCMP_FALSE) 1581a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson return Constant::getNullValue(ResultTy); 15827f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky 15837f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky if (pred == FCmpInst::FCMP_TRUE) 1584a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson return Constant::getAllOnesValue(ResultTy); 15857f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky 1586e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer // Handle some degenerate cases first 15870dd3549edc91b50fba102a6b0473caf8d4cf6b70Dan Gohman if (isa<UndefValue>(C1) || isa<UndefValue>(C2)) { 15880dd3549edc91b50fba102a6b0473caf8d4cf6b70Dan Gohman // For EQ and NE, we can always pick a value for the undef to make the 15890dd3549edc91b50fba102a6b0473caf8d4cf6b70Dan Gohman // predicate pass or fail, so we can return undef. 159068c0dbc14fb7599987fb3e27be4e12c1ac36535eDan Gohman // Also, if both operands are undef, we can return undef. 159168c0dbc14fb7599987fb3e27be4e12c1ac36535eDan Gohman if (ICmpInst::isEquality(ICmpInst::Predicate(pred)) || 159268c0dbc14fb7599987fb3e27be4e12c1ac36535eDan Gohman (isa<UndefValue>(C1) && isa<UndefValue>(C2))) 15930dd3549edc91b50fba102a6b0473caf8d4cf6b70Dan Gohman return UndefValue::get(ResultTy); 15940dd3549edc91b50fba102a6b0473caf8d4cf6b70Dan Gohman // Otherwise, pick the same value as the non-undef operand, and fold 15950dd3549edc91b50fba102a6b0473caf8d4cf6b70Dan Gohman // it to true or false. 1596c8e14b3d37b80abb6adb4b831af0452d9ecbf2b2Chris Lattner return ConstantInt::get(ResultTy, CmpInst::isTrueWhenEqual(pred)); 15970dd3549edc91b50fba102a6b0473caf8d4cf6b70Dan Gohman } 1598e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer 1599e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer // icmp eq/ne(null,GV) -> false/true 1600e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer if (C1->isNullValue()) { 1601e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2)) 1602780164433250ebc6f42671f5fdd259db14d71c0eDuncan Sands // Don't try to evaluate aliases. External weak GV can be null. 1603ae9f3a3b7c915f725aef5a7250e88eaeddda03c6Anton Korobeynikov if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) { 1604b913bbaa415299b732d72239566e203fe57761d6Reid Spencer if (pred == ICmpInst::ICMP_EQ) 1605b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::getFalse(C1->getContext()); 1606b913bbaa415299b732d72239566e203fe57761d6Reid Spencer else if (pred == ICmpInst::ICMP_NE) 1607b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::getTrue(C1->getContext()); 1608ae9f3a3b7c915f725aef5a7250e88eaeddda03c6Anton Korobeynikov } 1609e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer // icmp eq/ne(GV,null) -> false/true 1610e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer } else if (C2->isNullValue()) { 1611e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1)) 1612780164433250ebc6f42671f5fdd259db14d71c0eDuncan Sands // Don't try to evaluate aliases. External weak GV can be null. 1613ae9f3a3b7c915f725aef5a7250e88eaeddda03c6Anton Korobeynikov if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage()) { 1614b913bbaa415299b732d72239566e203fe57761d6Reid Spencer if (pred == ICmpInst::ICMP_EQ) 1615b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::getFalse(C1->getContext()); 1616b913bbaa415299b732d72239566e203fe57761d6Reid Spencer else if (pred == ICmpInst::ICMP_NE) 1617b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::getTrue(C1->getContext()); 1618ae9f3a3b7c915f725aef5a7250e88eaeddda03c6Anton Korobeynikov } 1619eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner } 1620eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner 162185958b059dcc4c1e91bb892d6cfe5987109a60acNick Lewycky // If the comparison is a comparison between two i1's, simplify it. 1622b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands if (C1->getType()->isIntegerTy(1)) { 162385958b059dcc4c1e91bb892d6cfe5987109a60acNick Lewycky switch(pred) { 162485958b059dcc4c1e91bb892d6cfe5987109a60acNick Lewycky case ICmpInst::ICMP_EQ: 162585958b059dcc4c1e91bb892d6cfe5987109a60acNick Lewycky if (isa<ConstantInt>(C2)) 162685958b059dcc4c1e91bb892d6cfe5987109a60acNick Lewycky return ConstantExpr::getXor(C1, ConstantExpr::getNot(C2)); 162785958b059dcc4c1e91bb892d6cfe5987109a60acNick Lewycky return ConstantExpr::getXor(ConstantExpr::getNot(C1), C2); 162885958b059dcc4c1e91bb892d6cfe5987109a60acNick Lewycky case ICmpInst::ICMP_NE: 162985958b059dcc4c1e91bb892d6cfe5987109a60acNick Lewycky return ConstantExpr::getXor(C1, C2); 163085958b059dcc4c1e91bb892d6cfe5987109a60acNick Lewycky default: 163185958b059dcc4c1e91bb892d6cfe5987109a60acNick Lewycky break; 163285958b059dcc4c1e91bb892d6cfe5987109a60acNick Lewycky } 163385958b059dcc4c1e91bb892d6cfe5987109a60acNick Lewycky } 163485958b059dcc4c1e91bb892d6cfe5987109a60acNick Lewycky 1635d333d906734088b074806721ee16aa68359e41c3Chris Lattner if (isa<ConstantInt>(C1) && isa<ConstantInt>(C2)) { 16369472c373a3378982ab4ef1a2caafabd4acf58ba2Reid Spencer APInt V1 = cast<ConstantInt>(C1)->getValue(); 16379472c373a3378982ab4ef1a2caafabd4acf58ba2Reid Spencer APInt V2 = cast<ConstantInt>(C2)->getValue(); 16389472c373a3378982ab4ef1a2caafabd4acf58ba2Reid Spencer switch (pred) { 16394d6ccb5f68cd7c6418a209f1fa4dbade569e4493David Blaikie default: llvm_unreachable("Invalid ICmp Predicate"); 1640b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner case ICmpInst::ICMP_EQ: return ConstantInt::get(ResultTy, V1 == V2); 1641b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner case ICmpInst::ICMP_NE: return ConstantInt::get(ResultTy, V1 != V2); 1642b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner case ICmpInst::ICMP_SLT: return ConstantInt::get(ResultTy, V1.slt(V2)); 1643b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner case ICmpInst::ICMP_SGT: return ConstantInt::get(ResultTy, V1.sgt(V2)); 1644b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner case ICmpInst::ICMP_SLE: return ConstantInt::get(ResultTy, V1.sle(V2)); 1645b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner case ICmpInst::ICMP_SGE: return ConstantInt::get(ResultTy, V1.sge(V2)); 1646b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner case ICmpInst::ICMP_ULT: return ConstantInt::get(ResultTy, V1.ult(V2)); 1647b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner case ICmpInst::ICMP_UGT: return ConstantInt::get(ResultTy, V1.ugt(V2)); 1648b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner case ICmpInst::ICMP_ULE: return ConstantInt::get(ResultTy, V1.ule(V2)); 1649b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner case ICmpInst::ICMP_UGE: return ConstantInt::get(ResultTy, V1.uge(V2)); 1650e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer } 1651e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer } else if (isa<ConstantFP>(C1) && isa<ConstantFP>(C2)) { 165243421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen APFloat C1V = cast<ConstantFP>(C1)->getValueAPF(); 165343421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen APFloat C2V = cast<ConstantFP>(C2)->getValueAPF(); 165443421b3dd70af5b70e71816521f37502c397cc65Dale Johannesen APFloat::cmpResult R = C1V.compare(C2V); 1655b913bbaa415299b732d72239566e203fe57761d6Reid Spencer switch (pred) { 16564d6ccb5f68cd7c6418a209f1fa4dbade569e4493David Blaikie default: llvm_unreachable("Invalid FCmp Predicate"); 1657b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner case FCmpInst::FCMP_FALSE: return Constant::getNullValue(ResultTy); 1658b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner case FCmpInst::FCMP_TRUE: return Constant::getAllOnesValue(ResultTy); 1659e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case FCmpInst::FCMP_UNO: 1660b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered); 166153054785115a4e7b521251fd698b6b82ee2c5c8fReid Spencer case FCmpInst::FCMP_ORD: 1662b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(ResultTy, R!=APFloat::cmpUnordered); 1663e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case FCmpInst::FCMP_UEQ: 1664b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered || 1665b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner R==APFloat::cmpEqual); 1666579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer case FCmpInst::FCMP_OEQ: 1667b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(ResultTy, R==APFloat::cmpEqual); 166853054785115a4e7b521251fd698b6b82ee2c5c8fReid Spencer case FCmpInst::FCMP_UNE: 1669b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(ResultTy, R!=APFloat::cmpEqual); 1670579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer case FCmpInst::FCMP_ONE: 1671b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan || 1672b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner R==APFloat::cmpGreaterThan); 167353054785115a4e7b521251fd698b6b82ee2c5c8fReid Spencer case FCmpInst::FCMP_ULT: 1674b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered || 1675b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner R==APFloat::cmpLessThan); 1676579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer case FCmpInst::FCMP_OLT: 1677b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan); 1678e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case FCmpInst::FCMP_UGT: 1679b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(ResultTy, R==APFloat::cmpUnordered || 1680b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner R==APFloat::cmpGreaterThan); 1681579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer case FCmpInst::FCMP_OGT: 1682b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(ResultTy, R==APFloat::cmpGreaterThan); 168353054785115a4e7b521251fd698b6b82ee2c5c8fReid Spencer case FCmpInst::FCMP_ULE: 1684b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(ResultTy, R!=APFloat::cmpGreaterThan); 1685579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer case FCmpInst::FCMP_OLE: 1686b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(ResultTy, R==APFloat::cmpLessThan || 1687b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner R==APFloat::cmpEqual); 1688e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case FCmpInst::FCMP_UGE: 1689b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(ResultTy, R!=APFloat::cmpLessThan); 1690579dca12c2cfd60bc18aaadbd5331897d48fec29Reid Spencer case FCmpInst::FCMP_OGE: 1691b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(ResultTy, R==APFloat::cmpGreaterThan || 1692b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner R==APFloat::cmpEqual); 1693e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer } 16941df9859c40492511b8aa4321eb76496005d3b75bDuncan Sands } else if (C1->getType()->isVectorTy()) { 1695fea85c46c0baae0d40584e3901607ed3db55d6efChris Lattner // If we can constant fold the comparison of each element, constant fold 1696fea85c46c0baae0d40584e3901607ed3db55d6efChris Lattner // the whole vector comparison. 1697fea85c46c0baae0d40584e3901607ed3db55d6efChris Lattner SmallVector<Constant*, 4> ResElts; 169803e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman Type *Ty = IntegerType::get(C1->getContext(), 32); 16992ca5c8644e6c35b3a7910a576ed89cddb7b82c3bChris Lattner // Compare the elements, producing an i1 result or constant expr. 170056243b89e7d5072d2d5498f806679d19ea483dacChris Lattner for (unsigned i = 0, e = C1->getType()->getVectorNumElements(); i != e;++i){ 170103e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman Constant *C1E = 170203e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman ConstantExpr::getExtractElement(C1, ConstantInt::get(Ty, i)); 170303e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman Constant *C2E = 170403e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman ConstantExpr::getExtractElement(C2, ConstantInt::get(Ty, i)); 170556243b89e7d5072d2d5498f806679d19ea483dacChris Lattner 170656243b89e7d5072d2d5498f806679d19ea483dacChris Lattner ResElts.push_back(ConstantExpr::getCompare(pred, C1E, C2E)); 170756243b89e7d5072d2d5498f806679d19ea483dacChris Lattner } 170856243b89e7d5072d2d5498f806679d19ea483dacChris Lattner 170903e091f0b5f43beee12170efc00bbab86ffeb0dcDan Gohman return ConstantVector::get(ResElts); 1710e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer } 1711504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner 1712b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands if (C1->getType()->isFloatingPointTy()) { 17138013954f15e8de25e1231de9665a702a864c2b8fChris Lattner int Result = -1; // -1 = unknown, 0 = known false, 1 = known true. 1714b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner switch (evaluateFCmpRelation(C1, C2)) { 1715c23197a26f34f559ea9797de51e187087c039c42Torok Edwin default: llvm_unreachable("Unknown relation!"); 1716b913bbaa415299b732d72239566e203fe57761d6Reid Spencer case FCmpInst::FCMP_UNO: 1717b913bbaa415299b732d72239566e203fe57761d6Reid Spencer case FCmpInst::FCMP_ORD: 1718b913bbaa415299b732d72239566e203fe57761d6Reid Spencer case FCmpInst::FCMP_UEQ: 1719b913bbaa415299b732d72239566e203fe57761d6Reid Spencer case FCmpInst::FCMP_UNE: 1720b913bbaa415299b732d72239566e203fe57761d6Reid Spencer case FCmpInst::FCMP_ULT: 1721b913bbaa415299b732d72239566e203fe57761d6Reid Spencer case FCmpInst::FCMP_UGT: 1722b913bbaa415299b732d72239566e203fe57761d6Reid Spencer case FCmpInst::FCMP_ULE: 1723b913bbaa415299b732d72239566e203fe57761d6Reid Spencer case FCmpInst::FCMP_UGE: 1724b913bbaa415299b732d72239566e203fe57761d6Reid Spencer case FCmpInst::FCMP_TRUE: 1725b913bbaa415299b732d72239566e203fe57761d6Reid Spencer case FCmpInst::FCMP_FALSE: 1726b913bbaa415299b732d72239566e203fe57761d6Reid Spencer case FCmpInst::BAD_FCMP_PREDICATE: 1727b913bbaa415299b732d72239566e203fe57761d6Reid Spencer break; // Couldn't determine anything about these constants. 1728b913bbaa415299b732d72239566e203fe57761d6Reid Spencer case FCmpInst::FCMP_OEQ: // We know that C1 == C2 1729155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner Result = (pred == FCmpInst::FCMP_UEQ || pred == FCmpInst::FCMP_OEQ || 1730155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE || 1731155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE); 1732155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner break; 1733b913bbaa415299b732d72239566e203fe57761d6Reid Spencer case FCmpInst::FCMP_OLT: // We know that C1 < C2 1734155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE || 1735155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT || 1736155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner pred == FCmpInst::FCMP_ULE || pred == FCmpInst::FCMP_OLE); 1737155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner break; 1738b913bbaa415299b732d72239566e203fe57761d6Reid Spencer case FCmpInst::FCMP_OGT: // We know that C1 > C2 1739155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner Result = (pred == FCmpInst::FCMP_UNE || pred == FCmpInst::FCMP_ONE || 1740155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT || 1741155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner pred == FCmpInst::FCMP_UGE || pred == FCmpInst::FCMP_OGE); 1742155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner break; 1743b913bbaa415299b732d72239566e203fe57761d6Reid Spencer case FCmpInst::FCMP_OLE: // We know that C1 <= C2 1744b913bbaa415299b732d72239566e203fe57761d6Reid Spencer // We can only partially decide this relation. 1745b913bbaa415299b732d72239566e203fe57761d6Reid Spencer if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT) 1746155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner Result = 0; 1747155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner else if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT) 1748155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner Result = 1; 1749b913bbaa415299b732d72239566e203fe57761d6Reid Spencer break; 1750b913bbaa415299b732d72239566e203fe57761d6Reid Spencer case FCmpInst::FCMP_OGE: // We known that C1 >= C2 1751b913bbaa415299b732d72239566e203fe57761d6Reid Spencer // We can only partially decide this relation. 1752b913bbaa415299b732d72239566e203fe57761d6Reid Spencer if (pred == FCmpInst::FCMP_ULT || pred == FCmpInst::FCMP_OLT) 1753155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner Result = 0; 1754155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner else if (pred == FCmpInst::FCMP_UGT || pred == FCmpInst::FCMP_OGT) 1755155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner Result = 1; 1756b913bbaa415299b732d72239566e203fe57761d6Reid Spencer break; 1757bb25e2c91b79fc31103510860e1817863a674bc5Nick Lewycky case FCmpInst::FCMP_ONE: // We know that C1 != C2 1758b913bbaa415299b732d72239566e203fe57761d6Reid Spencer // We can only partially decide this relation. 1759b913bbaa415299b732d72239566e203fe57761d6Reid Spencer if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ) 1760155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner Result = 0; 1761155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner else if (pred == FCmpInst::FCMP_ONE || pred == FCmpInst::FCMP_UNE) 1762155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner Result = 1; 1763b913bbaa415299b732d72239566e203fe57761d6Reid Spencer break; 1764b913bbaa415299b732d72239566e203fe57761d6Reid Spencer } 1765f8a87e8343c5f829be9a75d8160e1bb969b84119Dan Gohman 1766155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner // If we evaluated the result, return it now. 17677f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky if (Result != -1) 1768b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(ResultTy, Result); 17697f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky 1770b913bbaa415299b732d72239566e203fe57761d6Reid Spencer } else { 1771b913bbaa415299b732d72239566e203fe57761d6Reid Spencer // Evaluate the relation between the two constants, per the predicate. 1772155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner int Result = -1; // -1 = unknown, 0 = known false, 1 = known true. 1773b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner switch (evaluateICmpRelation(C1, C2, CmpInst::isSigned(pred))) { 1774c23197a26f34f559ea9797de51e187087c039c42Torok Edwin default: llvm_unreachable("Unknown relational!"); 1775b913bbaa415299b732d72239566e203fe57761d6Reid Spencer case ICmpInst::BAD_ICMP_PREDICATE: 1776b913bbaa415299b732d72239566e203fe57761d6Reid Spencer break; // Couldn't determine anything about these constants. 1777b913bbaa415299b732d72239566e203fe57761d6Reid Spencer case ICmpInst::ICMP_EQ: // We know the constants are equal! 1778b913bbaa415299b732d72239566e203fe57761d6Reid Spencer // If we know the constants are equal, we can decide the result of this 1779b913bbaa415299b732d72239566e203fe57761d6Reid Spencer // computation precisely. 17803892baac42daadf760d140168720be1d6e585aa8Nick Lewycky Result = ICmpInst::isTrueWhenEqual((ICmpInst::Predicate)pred); 1781155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner break; 1782e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case ICmpInst::ICMP_ULT: 17833892baac42daadf760d140168720be1d6e585aa8Nick Lewycky switch (pred) { 17843892baac42daadf760d140168720be1d6e585aa8Nick Lewycky case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_ULE: 17858de003663db10137a6c3f3af284317295b9afac6Nick Lewycky Result = 1; break; 17863892baac42daadf760d140168720be1d6e585aa8Nick Lewycky case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_UGE: 17878de003663db10137a6c3f3af284317295b9afac6Nick Lewycky Result = 0; break; 17883892baac42daadf760d140168720be1d6e585aa8Nick Lewycky } 1789155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner break; 1790e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case ICmpInst::ICMP_SLT: 17913892baac42daadf760d140168720be1d6e585aa8Nick Lewycky switch (pred) { 17923892baac42daadf760d140168720be1d6e585aa8Nick Lewycky case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SLE: 17938de003663db10137a6c3f3af284317295b9afac6Nick Lewycky Result = 1; break; 17943892baac42daadf760d140168720be1d6e585aa8Nick Lewycky case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SGE: 17958de003663db10137a6c3f3af284317295b9afac6Nick Lewycky Result = 0; break; 17963892baac42daadf760d140168720be1d6e585aa8Nick Lewycky } 1797155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner break; 1798e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case ICmpInst::ICMP_UGT: 17993892baac42daadf760d140168720be1d6e585aa8Nick Lewycky switch (pred) { 18003892baac42daadf760d140168720be1d6e585aa8Nick Lewycky case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_UGE: 18018de003663db10137a6c3f3af284317295b9afac6Nick Lewycky Result = 1; break; 18023892baac42daadf760d140168720be1d6e585aa8Nick Lewycky case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_ULE: 18038de003663db10137a6c3f3af284317295b9afac6Nick Lewycky Result = 0; break; 18043892baac42daadf760d140168720be1d6e585aa8Nick Lewycky } 1805155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner break; 1806e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case ICmpInst::ICMP_SGT: 18073892baac42daadf760d140168720be1d6e585aa8Nick Lewycky switch (pred) { 18083892baac42daadf760d140168720be1d6e585aa8Nick Lewycky case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_NE: case ICmpInst::ICMP_SGE: 18098de003663db10137a6c3f3af284317295b9afac6Nick Lewycky Result = 1; break; 18103892baac42daadf760d140168720be1d6e585aa8Nick Lewycky case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_EQ: case ICmpInst::ICMP_SLE: 18118de003663db10137a6c3f3af284317295b9afac6Nick Lewycky Result = 0; break; 18123892baac42daadf760d140168720be1d6e585aa8Nick Lewycky } 1813155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner break; 1814e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case ICmpInst::ICMP_ULE: 1815155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner if (pred == ICmpInst::ICMP_UGT) Result = 0; 18163892baac42daadf760d140168720be1d6e585aa8Nick Lewycky if (pred == ICmpInst::ICMP_ULT || pred == ICmpInst::ICMP_ULE) Result = 1; 1817b913bbaa415299b732d72239566e203fe57761d6Reid Spencer break; 1818e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case ICmpInst::ICMP_SLE: 1819155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner if (pred == ICmpInst::ICMP_SGT) Result = 0; 18203892baac42daadf760d140168720be1d6e585aa8Nick Lewycky if (pred == ICmpInst::ICMP_SLT || pred == ICmpInst::ICMP_SLE) Result = 1; 1821b913bbaa415299b732d72239566e203fe57761d6Reid Spencer break; 1822e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case ICmpInst::ICMP_UGE: 1823155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner if (pred == ICmpInst::ICMP_ULT) Result = 0; 18243892baac42daadf760d140168720be1d6e585aa8Nick Lewycky if (pred == ICmpInst::ICMP_UGT || pred == ICmpInst::ICMP_UGE) Result = 1; 1825b913bbaa415299b732d72239566e203fe57761d6Reid Spencer break; 1826e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer case ICmpInst::ICMP_SGE: 1827155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner if (pred == ICmpInst::ICMP_SLT) Result = 0; 18283892baac42daadf760d140168720be1d6e585aa8Nick Lewycky if (pred == ICmpInst::ICMP_SGT || pred == ICmpInst::ICMP_SGE) Result = 1; 1829b913bbaa415299b732d72239566e203fe57761d6Reid Spencer break; 1830b913bbaa415299b732d72239566e203fe57761d6Reid Spencer case ICmpInst::ICMP_NE: 1831155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner if (pred == ICmpInst::ICMP_EQ) Result = 0; 1832155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner if (pred == ICmpInst::ICMP_NE) Result = 1; 1833504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner break; 1834504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner } 1835f8a87e8343c5f829be9a75d8160e1bb969b84119Dan Gohman 1836155a490258a3a58f77ead53daa2078c3898938b5Chris Lattner // If we evaluated the result, return it now. 18377f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky if (Result != -1) 1838b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner return ConstantInt::get(ResultTy, Result); 1839f8a87e8343c5f829be9a75d8160e1bb969b84119Dan Gohman 184055a722bf6a08c684e49f5b56301bfc66d6dc9fd4Nick Lewycky // If the right hand side is a bitcast, try using its inverse to simplify 18416304b0dd631ba65f3e325d8ed85a0f770d74d633Chris Lattner // it by moving it to the left hand side. We can't do this if it would turn 1842a419b56d896d5180adb14e39d5c969306bd5b400Duncan Sands // a vector compare into a scalar compare or visa versa. 184355a722bf6a08c684e49f5b56301bfc66d6dc9fd4Nick Lewycky if (ConstantExpr *CE2 = dyn_cast<ConstantExpr>(C2)) { 18446304b0dd631ba65f3e325d8ed85a0f770d74d633Chris Lattner Constant *CE2Op0 = CE2->getOperand(0); 18456304b0dd631ba65f3e325d8ed85a0f770d74d633Chris Lattner if (CE2->getOpcode() == Instruction::BitCast && 18465bf7f88ea28dae0cad4f311cc5ef708b6ce50e8fNick Lewycky CE2->getType()->isVectorTy() == CE2Op0->getType()->isVectorTy()) { 184755a722bf6a08c684e49f5b56301bfc66d6dc9fd4Nick Lewycky Constant *Inverse = ConstantExpr::getBitCast(C1, CE2Op0->getType()); 184855a722bf6a08c684e49f5b56301bfc66d6dc9fd4Nick Lewycky return ConstantExpr::getICmp(pred, Inverse, CE2Op0); 184955a722bf6a08c684e49f5b56301bfc66d6dc9fd4Nick Lewycky } 185055a722bf6a08c684e49f5b56301bfc66d6dc9fd4Nick Lewycky } 185155a722bf6a08c684e49f5b56301bfc66d6dc9fd4Nick Lewycky 1852149cbc2a2418e506c71abe7759c9c81eea68976bNick Lewycky // If the left hand side is an extension, try eliminating it. 1853149cbc2a2418e506c71abe7759c9c81eea68976bNick Lewycky if (ConstantExpr *CE1 = dyn_cast<ConstantExpr>(C1)) { 18545bf7f88ea28dae0cad4f311cc5ef708b6ce50e8fNick Lewycky if ((CE1->getOpcode() == Instruction::SExt && ICmpInst::isSigned(pred)) || 18555bf7f88ea28dae0cad4f311cc5ef708b6ce50e8fNick Lewycky (CE1->getOpcode() == Instruction::ZExt && !ICmpInst::isSigned(pred))){ 1856149cbc2a2418e506c71abe7759c9c81eea68976bNick Lewycky Constant *CE1Op0 = CE1->getOperand(0); 1857149cbc2a2418e506c71abe7759c9c81eea68976bNick Lewycky Constant *CE1Inverse = ConstantExpr::getTrunc(CE1, CE1Op0->getType()); 1858149cbc2a2418e506c71abe7759c9c81eea68976bNick Lewycky if (CE1Inverse == CE1Op0) { 1859149cbc2a2418e506c71abe7759c9c81eea68976bNick Lewycky // Check whether we can safely truncate the right hand side. 1860149cbc2a2418e506c71abe7759c9c81eea68976bNick Lewycky Constant *C2Inverse = ConstantExpr::getTrunc(C2, CE1Op0->getType()); 186180c070493d657a4bcc6eca5627c28d659d3330d8Benjamin Kramer if (ConstantExpr::getCast(CE1->getOpcode(), C2Inverse, 186280c070493d657a4bcc6eca5627c28d659d3330d8Benjamin Kramer C2->getType()) == C2) 1863149cbc2a2418e506c71abe7759c9c81eea68976bNick Lewycky return ConstantExpr::getICmp(pred, CE1Inverse, C2Inverse); 1864149cbc2a2418e506c71abe7759c9c81eea68976bNick Lewycky } 1865149cbc2a2418e506c71abe7759c9c81eea68976bNick Lewycky } 1866149cbc2a2418e506c71abe7759c9c81eea68976bNick Lewycky } 1867149cbc2a2418e506c71abe7759c9c81eea68976bNick Lewycky 1868e8e17833bec5943dffe5f89546465f94d8277ad6Eli Friedman if ((!isa<ConstantExpr>(C1) && isa<ConstantExpr>(C2)) || 1869e8e17833bec5943dffe5f89546465f94d8277ad6Eli Friedman (C1->isNullValue() && !C2->isNullValue())) { 18707f6aa2b162e5daaf7b9ccf05d749597d3d7cf460Nick Lewycky // If C2 is a constant expr and C1 isn't, flip them around and fold the 1871b913bbaa415299b732d72239566e203fe57761d6Reid Spencer // other way if possible. 1872e8e17833bec5943dffe5f89546465f94d8277ad6Eli Friedman // Also, if C1 is null and C2 isn't, flip them around. 18735bf7f88ea28dae0cad4f311cc5ef708b6ce50e8fNick Lewycky pred = ICmpInst::getSwappedPredicate((ICmpInst::Predicate)pred); 18745bf7f88ea28dae0cad4f311cc5ef708b6ce50e8fNick Lewycky return ConstantExpr::getICmp(pred, C2, C1); 1875b913bbaa415299b732d72239566e203fe57761d6Reid Spencer } 1876504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner } 1877504e8fb74e4eec97a2ee1c195026824468cb6f7fChris Lattner return 0; 1878f8a87e8343c5f829be9a75d8160e1bb969b84119Dan Gohman} 1879eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner 18803bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman/// isInBoundsIndices - Test whether the given sequence of *normalized* indices 18813bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman/// is "inbounds". 188225052d8d64f18a85d6a84e0e010f6ba3eba0760dJay Foadtemplate<typename IndexTy> 1883dab3d29605a5c83db41b28176273ef55961120c1Jay Foadstatic bool isInBoundsIndices(ArrayRef<IndexTy> Idxs) { 18843bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman // No indices means nothing that could be out of bounds. 1885dab3d29605a5c83db41b28176273ef55961120c1Jay Foad if (Idxs.empty()) return true; 18863bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman 18873bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman // If the first index is zero, it's in bounds. 188825052d8d64f18a85d6a84e0e010f6ba3eba0760dJay Foad if (cast<Constant>(Idxs[0])->isNullValue()) return true; 18893bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman 18903bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman // If the first index is one and all the rest are zero, it's in bounds, 18913bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman // by the one-past-the-end rule. 18923bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman if (!cast<ConstantInt>(Idxs[0])->isOne()) 18933bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman return false; 1894dab3d29605a5c83db41b28176273ef55961120c1Jay Foad for (unsigned i = 1, e = Idxs.size(); i != e; ++i) 189525052d8d64f18a85d6a84e0e010f6ba3eba0760dJay Foad if (!cast<Constant>(Idxs[i])->isNullValue()) 18963bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman return false; 18973bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman return true; 18983bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman} 18993bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman 190025052d8d64f18a85d6a84e0e010f6ba3eba0760dJay Foadtemplate<typename IndexTy> 190125052d8d64f18a85d6a84e0e010f6ba3eba0760dJay Foadstatic Constant *ConstantFoldGetElementPtrImpl(Constant *C, 190225052d8d64f18a85d6a84e0e010f6ba3eba0760dJay Foad bool inBounds, 19037fc52e2d95ffa543288a2fef7ef1346ce14b1a61Jay Foad ArrayRef<IndexTy> Idxs) { 19047fc52e2d95ffa543288a2fef7ef1346ce14b1a61Jay Foad if (Idxs.empty()) return C; 190525052d8d64f18a85d6a84e0e010f6ba3eba0760dJay Foad Constant *Idx0 = cast<Constant>(Idxs[0]); 19067fc52e2d95ffa543288a2fef7ef1346ce14b1a61Jay Foad if ((Idxs.size() == 1 && Idx0->isNullValue())) 190733c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky return C; 1908eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner 1909cfbf9faaa4f1c92a746c8681ecd3bc52ad5313e8Chris Lattner if (isa<UndefValue>(C)) { 1910db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner PointerType *Ptr = cast<PointerType>(C->getType()); 1911a9203109f4ac95aa7e9624f2838e3d89623ec902Jay Foad Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs); 1912cfbf9faaa4f1c92a746c8681ecd3bc52ad5313e8Chris Lattner assert(Ty != 0 && "Invalid indices for GEP!"); 19139e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson return UndefValue::get(PointerType::get(Ty, Ptr->getAddressSpace())); 1914cfbf9faaa4f1c92a746c8681ecd3bc52ad5313e8Chris Lattner } 1915cfbf9faaa4f1c92a746c8681ecd3bc52ad5313e8Chris Lattner 19162e9bb1a88e806644e96b2a73bf8c2eb540d68edeChris Lattner if (C->isNullValue()) { 19172e9bb1a88e806644e96b2a73bf8c2eb540d68edeChris Lattner bool isNull = true; 19187fc52e2d95ffa543288a2fef7ef1346ce14b1a61Jay Foad for (unsigned i = 0, e = Idxs.size(); i != e; ++i) 191925052d8d64f18a85d6a84e0e010f6ba3eba0760dJay Foad if (!cast<Constant>(Idxs[i])->isNullValue()) { 19202e9bb1a88e806644e96b2a73bf8c2eb540d68edeChris Lattner isNull = false; 19212e9bb1a88e806644e96b2a73bf8c2eb540d68edeChris Lattner break; 19222e9bb1a88e806644e96b2a73bf8c2eb540d68edeChris Lattner } 19232e9bb1a88e806644e96b2a73bf8c2eb540d68edeChris Lattner if (isNull) { 1924db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner PointerType *Ptr = cast<PointerType>(C->getType()); 1925a9203109f4ac95aa7e9624f2838e3d89623ec902Jay Foad Type *Ty = GetElementPtrInst::getIndexedType(Ptr, Idxs); 19262e9bb1a88e806644e96b2a73bf8c2eb540d68edeChris Lattner assert(Ty != 0 && "Invalid indices for GEP!"); 1927b8787f340326100288aeda455ee8dbee7ab2f209Nick Lewycky return ConstantPointerNull::get(PointerType::get(Ty, 1928b8787f340326100288aeda455ee8dbee7ab2f209Nick Lewycky Ptr->getAddressSpace())); 19292e9bb1a88e806644e96b2a73bf8c2eb540d68edeChris Lattner } 19302e9bb1a88e806644e96b2a73bf8c2eb540d68edeChris Lattner } 1931eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner 193233c06adcf10d3ef7bbfc70f09f4966eec2e6c85eNick Lewycky if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { 1933eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner // Combine Indices - If the source pointer to this getelementptr instruction 1934eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner // is a getelementptr instruction, combine the indices of the two 1935eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner // getelementptr instructions into a single instruction. 1936eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner // 1937eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner if (CE->getOpcode() == Instruction::GetElementPtr) { 1938db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *LastTy = 0; 1939eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner for (gep_type_iterator I = gep_type_begin(CE), E = gep_type_end(CE); 1940eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner I != E; ++I) 1941eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner LastTy = *I; 1942eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner 1943adeb0a6e64c5a63e891e016226645f9595d00959Eli Friedman if ((LastTy && isa<SequentialType>(LastTy)) || Idx0->isNullValue()) { 19442b9a5daf7c4dc39b46662a69e95e610e7b2cd4baChris Lattner SmallVector<Value*, 16> NewIndices; 19457fc52e2d95ffa543288a2fef7ef1346ce14b1a61Jay Foad NewIndices.reserve(Idxs.size() + CE->getNumOperands()); 1946eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner for (unsigned i = 1, e = CE->getNumOperands()-1; i != e; ++i) 19477fa6e666ece60455cf9d75eff6e6915bebf05cbcChris Lattner NewIndices.push_back(CE->getOperand(i)); 1948eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner 1949eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner // Add the last index of the source with the first index of the new GEP. 1950eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner // Make sure to handle the case when they are actually different types. 1951eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner Constant *Combined = CE->getOperand(CE->getNumOperands()-1); 19527fa6e666ece60455cf9d75eff6e6915bebf05cbcChris Lattner // Otherwise it must be an array. 19537fa6e666ece60455cf9d75eff6e6915bebf05cbcChris Lattner if (!Idx0->isNullValue()) { 1954db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *IdxTy = Combined->getType(); 1955575d95ce373f1e405e6c27ce8d9f244bba3bdd0dReid Spencer if (IdxTy != Idx0->getType()) { 1956db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *Int64Ty = Type::getInt64Ty(IdxTy->getContext()); 1957b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner Constant *C1 = ConstantExpr::getSExtOrBitCast(Idx0, Int64Ty); 1958b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner Constant *C2 = ConstantExpr::getSExtOrBitCast(Combined, Int64Ty); 1959baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson Combined = ConstantExpr::get(Instruction::Add, C1, C2); 1960575d95ce373f1e405e6c27ce8d9f244bba3bdd0dReid Spencer } else { 1961575d95ce373f1e405e6c27ce8d9f244bba3bdd0dReid Spencer Combined = 1962baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson ConstantExpr::get(Instruction::Add, Idx0, Combined); 1963575d95ce373f1e405e6c27ce8d9f244bba3bdd0dReid Spencer } 1964d34086796485ffcba4789ddda4277afca3d4f6b9Chris Lattner } 1965fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 1966eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner NewIndices.push_back(Combined); 19677fc52e2d95ffa543288a2fef7ef1346ce14b1a61Jay Foad NewIndices.append(Idxs.begin() + 1, Idxs.end()); 19684b5e207bf24ea9799547a0634acaf7398b32897cJay Foad return 19694b5e207bf24ea9799547a0634acaf7398b32897cJay Foad ConstantExpr::getGetElementPtr(CE->getOperand(0), NewIndices, 19704b5e207bf24ea9799547a0634acaf7398b32897cJay Foad inBounds && 19714b5e207bf24ea9799547a0634acaf7398b32897cJay Foad cast<GEPOperator>(CE)->isInBounds()); 1972eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner } 1973eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner } 1974eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner 19758df7c39976890d94198d835e032848a374fec158Meador Inge // Attempt to fold casts to the same type away. For example, folding: 1976eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner // 19778df7c39976890d94198d835e032848a374fec158Meador Inge // i32* getelementptr ([2 x i32]* bitcast ([3 x i32]* %X to [2 x i32]*), 19788df7c39976890d94198d835e032848a374fec158Meador Inge // i64 0, i64 0) 19798df7c39976890d94198d835e032848a374fec158Meador Inge // into: 19808df7c39976890d94198d835e032848a374fec158Meador Inge // 19818df7c39976890d94198d835e032848a374fec158Meador Inge // i32* getelementptr ([3 x i32]* %X, i64 0, i64 0) 19828df7c39976890d94198d835e032848a374fec158Meador Inge // 19838df7c39976890d94198d835e032848a374fec158Meador Inge // Don't fold if the cast is changing address spaces. 19847fc52e2d95ffa543288a2fef7ef1346ce14b1a61Jay Foad if (CE->isCast() && Idxs.size() > 1 && Idx0->isNullValue()) { 19858df7c39976890d94198d835e032848a374fec158Meador Inge PointerType *SrcPtrTy = 19868df7c39976890d94198d835e032848a374fec158Meador Inge dyn_cast<PointerType>(CE->getOperand(0)->getType()); 19878df7c39976890d94198d835e032848a374fec158Meador Inge PointerType *DstPtrTy = dyn_cast<PointerType>(CE->getType()); 19888df7c39976890d94198d835e032848a374fec158Meador Inge if (SrcPtrTy && DstPtrTy) { 19898df7c39976890d94198d835e032848a374fec158Meador Inge ArrayType *SrcArrayTy = 19908df7c39976890d94198d835e032848a374fec158Meador Inge dyn_cast<ArrayType>(SrcPtrTy->getElementType()); 19918df7c39976890d94198d835e032848a374fec158Meador Inge ArrayType *DstArrayTy = 19928df7c39976890d94198d835e032848a374fec158Meador Inge dyn_cast<ArrayType>(DstPtrTy->getElementType()); 19938df7c39976890d94198d835e032848a374fec158Meador Inge if (SrcArrayTy && DstArrayTy 19948df7c39976890d94198d835e032848a374fec158Meador Inge && SrcArrayTy->getElementType() == DstArrayTy->getElementType() 19958df7c39976890d94198d835e032848a374fec158Meador Inge && SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace()) 19968df7c39976890d94198d835e032848a374fec158Meador Inge return ConstantExpr::getGetElementPtr((Constant*)CE->getOperand(0), 19978df7c39976890d94198d835e032848a374fec158Meador Inge Idxs, inBounds); 19988df7c39976890d94198d835e032848a374fec158Meador Inge } 1999fe9d82a71bac418d82f14d28d654dedea66083d5Chris Lattner } 2000eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner } 20013bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman 20023bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman // Check to see if any array indices are not within the corresponding 20033bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman // notional array bounds. If so, try to determine if they can be factored 20043bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman // out into preceding dimensions. 20053bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman bool Unknown = false; 20063bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman SmallVector<Constant *, 8> NewIdxs; 2007db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *Ty = C->getType(); 2008db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *Prev = 0; 20097fc52e2d95ffa543288a2fef7ef1346ce14b1a61Jay Foad for (unsigned i = 0, e = Idxs.size(); i != e; 20103bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman Prev = Ty, Ty = cast<CompositeType>(Ty)->getTypeAtIndex(Idxs[i]), ++i) { 20113bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman if (ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) { 2012db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) 20133bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman if (ATy->getNumElements() <= INT64_MAX && 20143bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman ATy->getNumElements() != 0 && 20153bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman CI->getSExtValue() >= (int64_t)ATy->getNumElements()) { 20163bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman if (isa<SequentialType>(Prev)) { 20173bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman // It's out of range, but we can factor it into the prior 20183bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman // dimension. 20197fc52e2d95ffa543288a2fef7ef1346ce14b1a61Jay Foad NewIdxs.resize(Idxs.size()); 20203bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman ConstantInt *Factor = ConstantInt::get(CI->getType(), 20213bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman ATy->getNumElements()); 20223bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman NewIdxs[i] = ConstantExpr::getSRem(CI, Factor); 20233bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman 202425052d8d64f18a85d6a84e0e010f6ba3eba0760dJay Foad Constant *PrevIdx = cast<Constant>(Idxs[i-1]); 20253bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman Constant *Div = ConstantExpr::getSDiv(CI, Factor); 20263bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman 20273bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman // Before adding, extend both operands to i64 to avoid 20283bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman // overflow trouble. 2029b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands if (!PrevIdx->getType()->isIntegerTy(64)) 20303bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman PrevIdx = ConstantExpr::getSExt(PrevIdx, 2031b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner Type::getInt64Ty(Div->getContext())); 2032b0bc6c361da9009e8414efde317d9bbff755f6c0Duncan Sands if (!Div->getType()->isIntegerTy(64)) 20333bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman Div = ConstantExpr::getSExt(Div, 2034b29d596072d2ba32a0bb59241baf7a1c0ff5ed94Chris Lattner Type::getInt64Ty(Div->getContext())); 20353bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman 20363bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman NewIdxs[i-1] = ConstantExpr::getAdd(PrevIdx, Div); 20373bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman } else { 20383bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman // It's out of range, but the prior dimension is a struct 20393bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman // so we can't do anything about it. 20403bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman Unknown = true; 20413bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman } 20423bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman } 20433bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman } else { 20443bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman // We don't know if it's in range or not. 20453bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman Unknown = true; 20463bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman } 20473bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman } 20483bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman 20493bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman // If we did any factoring, start over with the adjusted indices. 20503bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman if (!NewIdxs.empty()) { 20517fc52e2d95ffa543288a2fef7ef1346ce14b1a61Jay Foad for (unsigned i = 0, e = Idxs.size(); i != e; ++i) 205225052d8d64f18a85d6a84e0e010f6ba3eba0760dJay Foad if (!NewIdxs[i]) NewIdxs[i] = cast<Constant>(Idxs[i]); 20534b5e207bf24ea9799547a0634acaf7398b32897cJay Foad return ConstantExpr::getGetElementPtr(C, NewIdxs, inBounds); 20543bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman } 20553bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman 20563bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman // If all indices are known integers and normalized, we can do a simple 20573bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman // check for the "inbounds" property. 20583bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman if (!Unknown && !inBounds && 2059dab3d29605a5c83db41b28176273ef55961120c1Jay Foad isa<GlobalVariable>(C) && isInBoundsIndices(Idxs)) 2060dab3d29605a5c83db41b28176273ef55961120c1Jay Foad return ConstantExpr::getInBoundsGetElementPtr(C, Idxs); 20613bfbc4587a7e79f08f8c126a9e62c3475fb90f8bDan Gohman 2062eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner return 0; 2063eab20b58b1ca882933b3934fd9f320aba0bc1f10Chris Lattner} 206425052d8d64f18a85d6a84e0e010f6ba3eba0760dJay Foad 206525052d8d64f18a85d6a84e0e010f6ba3eba0760dJay FoadConstant *llvm::ConstantFoldGetElementPtr(Constant *C, 206625052d8d64f18a85d6a84e0e010f6ba3eba0760dJay Foad bool inBounds, 20677fc52e2d95ffa543288a2fef7ef1346ce14b1a61Jay Foad ArrayRef<Constant *> Idxs) { 20687fc52e2d95ffa543288a2fef7ef1346ce14b1a61Jay Foad return ConstantFoldGetElementPtrImpl(C, inBounds, Idxs); 206925052d8d64f18a85d6a84e0e010f6ba3eba0760dJay Foad} 207025052d8d64f18a85d6a84e0e010f6ba3eba0760dJay Foad 207125052d8d64f18a85d6a84e0e010f6ba3eba0760dJay FoadConstant *llvm::ConstantFoldGetElementPtr(Constant *C, 207225052d8d64f18a85d6a84e0e010f6ba3eba0760dJay Foad bool inBounds, 20737fc52e2d95ffa543288a2fef7ef1346ce14b1a61Jay Foad ArrayRef<Value *> Idxs) { 20747fc52e2d95ffa543288a2fef7ef1346ce14b1a61Jay Foad return ConstantFoldGetElementPtrImpl(C, inBounds, Idxs); 207525052d8d64f18a85d6a84e0e010f6ba3eba0760dJay Foad} 2076