Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1 | //===-- Instructions.cpp - Implement the LLVM instructions ----------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 10 | // This file implements all of the non-inline methods for the LLVM instruction |
| 11 | // classes. |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 15 | #include "llvm/IR/Instructions.h" |
Devang Patel | 6c6c016 | 2009-09-23 18:32:25 +0000 | [diff] [blame] | 16 | #include "LLVMContextImpl.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Constants.h" |
| 18 | #include "llvm/IR/DataLayout.h" |
| 19 | #include "llvm/IR/DerivedTypes.h" |
| 20 | #include "llvm/IR/Function.h" |
| 21 | #include "llvm/IR/Module.h" |
| 22 | #include "llvm/IR/Operator.h" |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CallSite.h" |
Reid Spencer | 3da4384 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ConstantRange.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ErrorHandling.h" |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 26 | #include "llvm/Support/MathExtras.h" |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Chris Lattner | 50ee9dd | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
| 30 | // CallSite Class |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
Gabor Greif | c9f7500 | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 33 | User::op_iterator CallSite::getCallee() const { |
| 34 | Instruction *II(getInstruction()); |
| 35 | return isCall() |
Gabor Greif | 3ecf355 | 2010-08-05 21:25:49 +0000 | [diff] [blame] | 36 | ? cast<CallInst>(II)->op_end() - 1 // Skip Callee |
Gabor Greif | a6aac4c | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 37 | : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee |
Gabor Greif | c9f7500 | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 40 | //===----------------------------------------------------------------------===// |
| 41 | // TerminatorInst Class |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
| 44 | // Out of line virtual method, so the vtable, etc has a home. |
| 45 | TerminatorInst::~TerminatorInst() { |
| 46 | } |
| 47 | |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 48 | //===----------------------------------------------------------------------===// |
| 49 | // UnaryInstruction Class |
| 50 | //===----------------------------------------------------------------------===// |
| 51 | |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 52 | // Out of line virtual method, so the vtable, etc has a home. |
| 53 | UnaryInstruction::~UnaryInstruction() { |
| 54 | } |
| 55 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 56 | //===----------------------------------------------------------------------===// |
Chris Lattner | b76ec32 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 57 | // SelectInst Class |
| 58 | //===----------------------------------------------------------------------===// |
| 59 | |
| 60 | /// areInvalidOperands - Return a string if the specified operands are invalid |
| 61 | /// for a select operation, otherwise return null. |
| 62 | const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) { |
| 63 | if (Op1->getType() != Op2->getType()) |
| 64 | return "both values to select must have same type"; |
| 65 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 66 | if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) { |
Chris Lattner | b76ec32 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 67 | // Vector select. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 68 | if (VT->getElementType() != Type::getInt1Ty(Op0->getContext())) |
Chris Lattner | b76ec32 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 69 | return "vector select condition element type must be i1"; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 70 | VectorType *ET = dyn_cast<VectorType>(Op1->getType()); |
Chris Lattner | b76ec32 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 71 | if (ET == 0) |
| 72 | return "selected values for vector select must be vectors"; |
| 73 | if (ET->getNumElements() != VT->getNumElements()) |
| 74 | return "vector select requires selected vectors to have " |
| 75 | "the same vector length as select condition"; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 76 | } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) { |
Chris Lattner | b76ec32 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 77 | return "select condition must be i1 or <n x i1>"; |
| 78 | } |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | //===----------------------------------------------------------------------===// |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 84 | // PHINode Class |
| 85 | //===----------------------------------------------------------------------===// |
| 86 | |
| 87 | PHINode::PHINode(const PHINode &PN) |
| 88 | : Instruction(PN.getType(), Instruction::PHI, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 89 | allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()), |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 90 | ReservedSpace(PN.getNumOperands()) { |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 91 | std::copy(PN.op_begin(), PN.op_end(), op_begin()); |
| 92 | std::copy(PN.block_begin(), PN.block_end(), block_begin()); |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 93 | SubclassOptionalData = PN.SubclassOptionalData; |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 96 | PHINode::~PHINode() { |
Jay Foad | 1ed26ac | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 97 | dropHungoffUses(); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 100 | Use *PHINode::allocHungoffUses(unsigned N) const { |
| 101 | // Allocate the array of Uses of the incoming values, followed by a pointer |
| 102 | // (with bottom bit set) to the User, followed by the array of pointers to |
| 103 | // the incoming basic blocks. |
| 104 | size_t size = N * sizeof(Use) + sizeof(Use::UserRef) |
| 105 | + N * sizeof(BasicBlock*); |
| 106 | Use *Begin = static_cast<Use*>(::operator new(size)); |
| 107 | Use *End = Begin + N; |
| 108 | (void) new(End) Use::UserRef(const_cast<PHINode*>(this), 1); |
| 109 | return Use::initTags(Begin, End); |
| 110 | } |
| 111 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 112 | // removeIncomingValue - Remove an incoming value. This is useful if a |
| 113 | // predecessor basic block is deleted. |
| 114 | Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 115 | Value *Removed = getIncomingValue(Idx); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 116 | |
| 117 | // Move everything after this operand down. |
| 118 | // |
| 119 | // FIXME: we could just swap with the end of the list, then erase. However, |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 120 | // clients might not expect this to happen. The code as it is thrashes the |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 121 | // use/def lists, which is kinda lame. |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 122 | std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx); |
| 123 | std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 124 | |
| 125 | // Nuke the last value. |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 126 | Op<-1>().set(0); |
| 127 | --NumOperands; |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 128 | |
| 129 | // If the PHI node is dead, because it has zero entries, nuke it now. |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 130 | if (getNumOperands() == 0 && DeletePHIIfEmpty) { |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 131 | // If anyone is using this PHI, make them use a dummy value instead... |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 132 | replaceAllUsesWith(UndefValue::get(getType())); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 133 | eraseFromParent(); |
| 134 | } |
| 135 | return Removed; |
| 136 | } |
| 137 | |
Jay Foad | 8891ed7 | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 138 | /// growOperands - grow operands - This grows the operand list in response |
| 139 | /// to a push_back style of operation. This grows the number of ops by 1.5 |
| 140 | /// times. |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 141 | /// |
Jay Foad | 8891ed7 | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 142 | void PHINode::growOperands() { |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 143 | unsigned e = getNumOperands(); |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 144 | unsigned NumOps = e + e / 2; |
| 145 | if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common. |
| 146 | |
| 147 | Use *OldOps = op_begin(); |
| 148 | BasicBlock **OldBlocks = block_begin(); |
Jay Foad | 72f5f31 | 2011-06-20 14:38:01 +0000 | [diff] [blame] | 149 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 150 | ReservedSpace = NumOps; |
Jay Foad | 95c3e48 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 151 | OperandList = allocHungoffUses(ReservedSpace); |
| 152 | |
| 153 | std::copy(OldOps, OldOps + e, op_begin()); |
| 154 | std::copy(OldBlocks, OldBlocks + e, block_begin()); |
| 155 | |
Jay Foad | 1ed26ac | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 156 | Use::zap(OldOps, OldOps + e, true); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Nate Begeman | a83ba0f | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 159 | /// hasConstantValue - If the specified PHI node always merges together the same |
| 160 | /// value, return the value, otherwise return null. |
Duncan Sands | ff10341 | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 161 | Value *PHINode::hasConstantValue() const { |
| 162 | // Exploit the fact that phi nodes always have at least one entry. |
| 163 | Value *ConstantValue = getIncomingValue(0); |
| 164 | for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i) |
Nuno Lopes | 44d5c06 | 2012-07-03 17:10:28 +0000 | [diff] [blame] | 165 | if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) { |
| 166 | if (ConstantValue != this) |
| 167 | return 0; // Incoming values not all the same. |
| 168 | // The case where the first value is this PHI. |
| 169 | ConstantValue = getIncomingValue(i); |
| 170 | } |
Nuno Lopes | 0fd518b | 2012-07-03 21:15:40 +0000 | [diff] [blame] | 171 | if (ConstantValue == this) |
| 172 | return UndefValue::get(getType()); |
Duncan Sands | ff10341 | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 173 | return ConstantValue; |
Nate Begeman | a83ba0f | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 176 | //===----------------------------------------------------------------------===// |
| 177 | // LandingPadInst Implementation |
| 178 | //===----------------------------------------------------------------------===// |
| 179 | |
| 180 | LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn, |
| 181 | unsigned NumReservedValues, const Twine &NameStr, |
| 182 | Instruction *InsertBefore) |
| 183 | : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertBefore) { |
| 184 | init(PersonalityFn, 1 + NumReservedValues, NameStr); |
| 185 | } |
| 186 | |
| 187 | LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn, |
| 188 | unsigned NumReservedValues, const Twine &NameStr, |
| 189 | BasicBlock *InsertAtEnd) |
| 190 | : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertAtEnd) { |
| 191 | init(PersonalityFn, 1 + NumReservedValues, NameStr); |
| 192 | } |
| 193 | |
| 194 | LandingPadInst::LandingPadInst(const LandingPadInst &LP) |
| 195 | : Instruction(LP.getType(), Instruction::LandingPad, |
| 196 | allocHungoffUses(LP.getNumOperands()), LP.getNumOperands()), |
| 197 | ReservedSpace(LP.getNumOperands()) { |
| 198 | Use *OL = OperandList, *InOL = LP.OperandList; |
| 199 | for (unsigned I = 0, E = ReservedSpace; I != E; ++I) |
| 200 | OL[I] = InOL[I]; |
| 201 | |
| 202 | setCleanup(LP.isCleanup()); |
| 203 | } |
| 204 | |
| 205 | LandingPadInst::~LandingPadInst() { |
| 206 | dropHungoffUses(); |
| 207 | } |
| 208 | |
| 209 | LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn, |
| 210 | unsigned NumReservedClauses, |
| 211 | const Twine &NameStr, |
| 212 | Instruction *InsertBefore) { |
| 213 | return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr, |
| 214 | InsertBefore); |
| 215 | } |
| 216 | |
| 217 | LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn, |
| 218 | unsigned NumReservedClauses, |
| 219 | const Twine &NameStr, |
| 220 | BasicBlock *InsertAtEnd) { |
| 221 | return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr, |
| 222 | InsertAtEnd); |
| 223 | } |
| 224 | |
| 225 | void LandingPadInst::init(Value *PersFn, unsigned NumReservedValues, |
| 226 | const Twine &NameStr) { |
| 227 | ReservedSpace = NumReservedValues; |
| 228 | NumOperands = 1; |
| 229 | OperandList = allocHungoffUses(ReservedSpace); |
| 230 | OperandList[0] = PersFn; |
| 231 | setName(NameStr); |
| 232 | setCleanup(false); |
| 233 | } |
| 234 | |
| 235 | /// growOperands - grow operands - This grows the operand list in response to a |
| 236 | /// push_back style of operation. This grows the number of ops by 2 times. |
| 237 | void LandingPadInst::growOperands(unsigned Size) { |
| 238 | unsigned e = getNumOperands(); |
| 239 | if (ReservedSpace >= e + Size) return; |
| 240 | ReservedSpace = (e + Size / 2) * 2; |
| 241 | |
| 242 | Use *NewOps = allocHungoffUses(ReservedSpace); |
| 243 | Use *OldOps = OperandList; |
| 244 | for (unsigned i = 0; i != e; ++i) |
| 245 | NewOps[i] = OldOps[i]; |
| 246 | |
| 247 | OperandList = NewOps; |
| 248 | Use::zap(OldOps, OldOps + e, true); |
| 249 | } |
| 250 | |
| 251 | void LandingPadInst::addClause(Value *Val) { |
| 252 | unsigned OpNo = getNumOperands(); |
| 253 | growOperands(1); |
| 254 | assert(OpNo < ReservedSpace && "Growing didn't work!"); |
| 255 | ++NumOperands; |
| 256 | OperandList[OpNo] = Val; |
| 257 | } |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 258 | |
| 259 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 260 | // CallInst Implementation |
| 261 | //===----------------------------------------------------------------------===// |
| 262 | |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 263 | CallInst::~CallInst() { |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 266 | void CallInst::init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr) { |
| 267 | assert(NumOperands == Args.size() + 1 && "NumOperands not set up?"); |
Gabor Greif | a6aac4c | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 268 | Op<-1>() = Func; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 269 | |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 270 | #ifndef NDEBUG |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 271 | FunctionType *FTy = |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 272 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
| 273 | |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 274 | assert((Args.size() == FTy->getNumParams() || |
| 275 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && |
Chris Lattner | 9b4c96d | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 276 | "Calling a function with bad signature!"); |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 277 | |
| 278 | for (unsigned i = 0; i != Args.size(); ++i) |
Chris Lattner | 9b4c96d | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 279 | assert((i >= FTy->getNumParams() || |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 280 | FTy->getParamType(i) == Args[i]->getType()) && |
Chris Lattner | 9b4c96d | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 281 | "Calling a function with a bad signature!"); |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 282 | #endif |
| 283 | |
| 284 | std::copy(Args.begin(), Args.end(), op_begin()); |
| 285 | setName(NameStr); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 288 | void CallInst::init(Value *Func, const Twine &NameStr) { |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 289 | assert(NumOperands == 1 && "NumOperands not set up?"); |
Gabor Greif | a6aac4c | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 290 | Op<-1>() = Func; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 291 | |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 292 | #ifndef NDEBUG |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 293 | FunctionType *FTy = |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 294 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
| 295 | |
Chris Lattner | 2e21fce | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 296 | assert(FTy->getNumParams() == 0 && "Calling a function with bad signature"); |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 297 | #endif |
| 298 | |
| 299 | setName(NameStr); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 302 | CallInst::CallInst(Value *Func, const Twine &Name, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 303 | Instruction *InsertBefore) |
| 304 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 305 | ->getElementType())->getReturnType(), |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 306 | Instruction::Call, |
| 307 | OperandTraits<CallInst>::op_end(this) - 1, |
| 308 | 1, InsertBefore) { |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 309 | init(Func, Name); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 312 | CallInst::CallInst(Value *Func, const Twine &Name, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 313 | BasicBlock *InsertAtEnd) |
| 314 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 315 | ->getElementType())->getReturnType(), |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 316 | Instruction::Call, |
| 317 | OperandTraits<CallInst>::op_end(this) - 1, |
| 318 | 1, InsertAtEnd) { |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 319 | init(Func, Name); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 322 | CallInst::CallInst(const CallInst &CI) |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 323 | : Instruction(CI.getType(), Instruction::Call, |
| 324 | OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(), |
Chris Lattner | 58d7491 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 325 | CI.getNumOperands()) { |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 326 | setAttributes(CI.getAttributes()); |
Chris Lattner | cafe9bb | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 327 | setTailCall(CI.isTailCall()); |
| 328 | setCallingConv(CI.getCallingConv()); |
| 329 | |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 330 | std::copy(CI.op_begin(), CI.op_end(), op_begin()); |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 331 | SubclassOptionalData = CI.SubclassOptionalData; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 334 | void CallInst::addAttribute(unsigned i, Attribute attr) { |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 335 | AttributeSet PAL = getAttributes(); |
Bill Wendling | c416795 | 2012-10-14 07:35:59 +0000 | [diff] [blame] | 336 | PAL = PAL.addAttr(getContext(), i, attr); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 337 | setAttributes(PAL); |
Eric Christopher | 0bf7b41 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 340 | void CallInst::removeAttribute(unsigned i, Attribute attr) { |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 341 | AttributeSet PAL = getAttributes(); |
Bill Wendling | 5886b7b | 2012-10-14 06:39:53 +0000 | [diff] [blame] | 342 | PAL = PAL.removeAttr(getContext(), i, attr); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 343 | setAttributes(PAL); |
Duncan Sands | 2e033f3 | 2008-07-08 08:38:44 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Bill Wendling | 629fb82 | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 346 | bool CallInst::hasFnAttr(Attribute::AttrKind A) const { |
Bill Wendling | 94e94b3 | 2012-12-30 13:50:49 +0000 | [diff] [blame] | 347 | if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A)) |
Bill Wendling | 060f20a | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 348 | return true; |
| 349 | if (const Function *F = getCalledFunction()) |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 350 | return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A); |
Bill Wendling | 060f20a | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 351 | return false; |
| 352 | } |
| 353 | |
Bill Wendling | 629fb82 | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 354 | bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const { |
Bill Wendling | 94e94b3 | 2012-12-30 13:50:49 +0000 | [diff] [blame] | 355 | if (AttributeList.hasAttribute(i, A)) |
Bill Wendling | 847d165 | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 356 | return true; |
| 357 | if (const Function *F = getCalledFunction()) |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 358 | return F->getAttributes().hasAttribute(i, A); |
Bill Wendling | 365b9e0 | 2012-10-04 07:18:12 +0000 | [diff] [blame] | 359 | return false; |
| 360 | } |
| 361 | |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 362 | /// IsConstantOne - Return true only if val is constant int 1 |
| 363 | static bool IsConstantOne(Value *val) { |
| 364 | assert(val && "IsConstantOne does not work with NULL val"); |
| 365 | return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne(); |
| 366 | } |
| 367 | |
Nick Lewycky | 3fc35c5 | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 368 | static Instruction *createMalloc(Instruction *InsertBefore, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 369 | BasicBlock *InsertAtEnd, Type *IntPtrTy, |
| 370 | Type *AllocTy, Value *AllocSize, |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 371 | Value *ArraySize, Function *MallocF, |
| 372 | const Twine &Name) { |
Benjamin Kramer | b84c5ae | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 373 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 374 | "createMalloc needs either InsertBefore or InsertAtEnd"); |
| 375 | |
| 376 | // malloc(type) becomes: |
| 377 | // bitcast (i8* malloc(typeSize)) to type* |
| 378 | // malloc(type, arraySize) becomes: |
| 379 | // bitcast (i8 *malloc(typeSize*arraySize)) to type* |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 380 | if (!ArraySize) |
| 381 | ArraySize = ConstantInt::get(IntPtrTy, 1); |
| 382 | else if (ArraySize->getType() != IntPtrTy) { |
| 383 | if (InsertBefore) |
Victor Hernandez | 4d81d97 | 2009-11-07 00:36:50 +0000 | [diff] [blame] | 384 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, |
| 385 | "", InsertBefore); |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 386 | else |
Victor Hernandez | 4d81d97 | 2009-11-07 00:36:50 +0000 | [diff] [blame] | 387 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, |
| 388 | "", InsertAtEnd); |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 389 | } |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 390 | |
Benjamin Kramer | b84c5ae | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 391 | if (!IsConstantOne(ArraySize)) { |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 392 | if (IsConstantOne(AllocSize)) { |
| 393 | AllocSize = ArraySize; // Operand * 1 = Operand |
| 394 | } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) { |
| 395 | Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy, |
| 396 | false /*ZExt*/); |
| 397 | // Malloc arg is constant product of type size and array size |
| 398 | AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize)); |
| 399 | } else { |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 400 | // Multiply type size by the array size... |
| 401 | if (InsertBefore) |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 402 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, |
| 403 | "mallocsize", InsertBefore); |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 404 | else |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 405 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, |
| 406 | "mallocsize", InsertAtEnd); |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 407 | } |
Benjamin Kramer | b84c5ae | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 408 | } |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 409 | |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 410 | assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size"); |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 411 | // Create the call to Malloc. |
| 412 | BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; |
| 413 | Module* M = BB->getParent()->getParent(); |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 414 | Type *BPTy = Type::getInt8PtrTy(BB->getContext()); |
Victor Hernandez | f06dca2 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 415 | Value *MallocFunc = MallocF; |
| 416 | if (!MallocFunc) |
Victor Hernandez | 13ad5aa | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 417 | // prototype malloc as "void *malloc(size_t)" |
Victor Hernandez | f06dca2 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 418 | MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 419 | PointerType *AllocPtrType = PointerType::getUnqual(AllocTy); |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 420 | CallInst *MCall = NULL; |
Nick Lewycky | 3fc35c5 | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 421 | Instruction *Result = NULL; |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 422 | if (InsertBefore) { |
Victor Hernandez | f06dca2 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 423 | MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore); |
Victor Hernandez | 13ad5aa | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 424 | Result = MCall; |
| 425 | if (Result->getType() != AllocPtrType) |
| 426 | // Create a cast instruction to convert to the right type... |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 427 | Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore); |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 428 | } else { |
Victor Hernandez | f06dca2 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 429 | MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall"); |
Victor Hernandez | 13ad5aa | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 430 | Result = MCall; |
| 431 | if (Result->getType() != AllocPtrType) { |
| 432 | InsertAtEnd->getInstList().push_back(MCall); |
| 433 | // Create a cast instruction to convert to the right type... |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 434 | Result = new BitCastInst(MCall, AllocPtrType, Name); |
Victor Hernandez | 13ad5aa | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 435 | } |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 436 | } |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 437 | MCall->setTailCall(); |
Victor Hernandez | f06dca2 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 438 | if (Function *F = dyn_cast<Function>(MallocFunc)) { |
| 439 | MCall->setCallingConv(F->getCallingConv()); |
| 440 | if (!F->doesNotAlias(0)) F->setDoesNotAlias(0); |
| 441 | } |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 442 | assert(!MCall->getType()->isVoidTy() && "Malloc has void return type"); |
Victor Hernandez | 88d9839 | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 443 | |
Victor Hernandez | 13ad5aa | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 444 | return Result; |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | /// CreateMalloc - Generate the IR for a call to malloc: |
| 448 | /// 1. Compute the malloc call's argument as the specified type's size, |
| 449 | /// possibly multiplied by the array size if the array size is not |
| 450 | /// constant 1. |
| 451 | /// 2. Call malloc with that argument. |
| 452 | /// 3. Bitcast the result of the malloc call to the specified type. |
Nick Lewycky | 3fc35c5 | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 453 | Instruction *CallInst::CreateMalloc(Instruction *InsertBefore, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 454 | Type *IntPtrTy, Type *AllocTy, |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 455 | Value *AllocSize, Value *ArraySize, |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 456 | Function * MallocF, |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 457 | const Twine &Name) { |
| 458 | return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize, |
Chris Lattner | 5a30a85 | 2010-07-12 00:57:28 +0000 | [diff] [blame] | 459 | ArraySize, MallocF, Name); |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /// CreateMalloc - Generate the IR for a call to malloc: |
| 463 | /// 1. Compute the malloc call's argument as the specified type's size, |
| 464 | /// possibly multiplied by the array size if the array size is not |
| 465 | /// constant 1. |
| 466 | /// 2. Call malloc with that argument. |
| 467 | /// 3. Bitcast the result of the malloc call to the specified type. |
| 468 | /// Note: This function does not add the bitcast to the basic block, that is the |
| 469 | /// responsibility of the caller. |
Nick Lewycky | 3fc35c5 | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 470 | Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 471 | Type *IntPtrTy, Type *AllocTy, |
Victor Hernandez | 9d0b704 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 472 | Value *AllocSize, Value *ArraySize, |
| 473 | Function *MallocF, const Twine &Name) { |
| 474 | return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize, |
Victor Hernandez | 13ad5aa | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 475 | ArraySize, MallocF, Name); |
Evan Cheng | fabcb91 | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 476 | } |
Duncan Sands | afa3b6d | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 477 | |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 478 | static Instruction* createFree(Value* Source, Instruction *InsertBefore, |
| 479 | BasicBlock *InsertAtEnd) { |
| 480 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && |
| 481 | "createFree needs either InsertBefore or InsertAtEnd"); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 482 | assert(Source->getType()->isPointerTy() && |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 483 | "Can not free something of nonpointer type!"); |
| 484 | |
| 485 | BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; |
| 486 | Module* M = BB->getParent()->getParent(); |
| 487 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 488 | Type *VoidTy = Type::getVoidTy(M->getContext()); |
| 489 | Type *IntPtrTy = Type::getInt8PtrTy(M->getContext()); |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 490 | // prototype free as "void free(void*)" |
Chris Lattner | 47fc9d3 | 2009-11-09 07:12:01 +0000 | [diff] [blame] | 491 | Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL); |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 492 | CallInst* Result = NULL; |
| 493 | Value *PtrCast = Source; |
| 494 | if (InsertBefore) { |
| 495 | if (Source->getType() != IntPtrTy) |
| 496 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore); |
| 497 | Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore); |
| 498 | } else { |
| 499 | if (Source->getType() != IntPtrTy) |
| 500 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd); |
| 501 | Result = CallInst::Create(FreeFunc, PtrCast, ""); |
| 502 | } |
| 503 | Result->setTailCall(); |
Chris Lattner | 47fc9d3 | 2009-11-09 07:12:01 +0000 | [diff] [blame] | 504 | if (Function *F = dyn_cast<Function>(FreeFunc)) |
| 505 | Result->setCallingConv(F->getCallingConv()); |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 506 | |
| 507 | return Result; |
| 508 | } |
| 509 | |
| 510 | /// CreateFree - Generate the IR for a call to the builtin free function. |
Chris Lattner | 5a30a85 | 2010-07-12 00:57:28 +0000 | [diff] [blame] | 511 | Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) { |
| 512 | return createFree(Source, InsertBefore, NULL); |
Victor Hernandez | 66284e0 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /// CreateFree - Generate the IR for a call to the builtin free function. |
| 516 | /// Note: This function does not add the call to the basic block, that is the |
| 517 | /// responsibility of the caller. |
| 518 | Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) { |
| 519 | Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd); |
| 520 | assert(FreeCall && "CreateFree did not create a CallInst"); |
| 521 | return FreeCall; |
| 522 | } |
| 523 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 524 | //===----------------------------------------------------------------------===// |
| 525 | // InvokeInst Implementation |
| 526 | //===----------------------------------------------------------------------===// |
| 527 | |
| 528 | void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 529 | ArrayRef<Value *> Args, const Twine &NameStr) { |
| 530 | assert(NumOperands == 3 + Args.size() && "NumOperands not set up?"); |
Gabor Greif | c9f7500 | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 531 | Op<-3>() = Fn; |
| 532 | Op<-2>() = IfNormal; |
| 533 | Op<-1>() = IfException; |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 534 | |
| 535 | #ifndef NDEBUG |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 536 | FunctionType *FTy = |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 537 | cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 538 | |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 539 | assert(((Args.size() == FTy->getNumParams()) || |
| 540 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && |
Gabor Greif | 050560e | 2010-03-23 13:45:54 +0000 | [diff] [blame] | 541 | "Invoking a function with bad signature"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 542 | |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 543 | for (unsigned i = 0, e = Args.size(); i != e; i++) |
Chris Lattner | 9b4c96d | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 544 | assert((i >= FTy->getNumParams() || |
Chris Lattner | d2dd150 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 545 | FTy->getParamType(i) == Args[i]->getType()) && |
Chris Lattner | 9b4c96d | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 546 | "Invoking a function with a bad signature!"); |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 547 | #endif |
| 548 | |
| 549 | std::copy(Args.begin(), Args.end(), op_begin()); |
| 550 | setName(NameStr); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 553 | InvokeInst::InvokeInst(const InvokeInst &II) |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 554 | : TerminatorInst(II.getType(), Instruction::Invoke, |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 555 | OperandTraits<InvokeInst>::op_end(this) |
| 556 | - II.getNumOperands(), |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 557 | II.getNumOperands()) { |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 558 | setAttributes(II.getAttributes()); |
Chris Lattner | cafe9bb | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 559 | setCallingConv(II.getCallingConv()); |
Jay Foad | a3efbb1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 560 | std::copy(II.op_begin(), II.op_end(), op_begin()); |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 561 | SubclassOptionalData = II.SubclassOptionalData; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 564 | BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const { |
| 565 | return getSuccessor(idx); |
| 566 | } |
| 567 | unsigned InvokeInst::getNumSuccessorsV() const { |
| 568 | return getNumSuccessors(); |
| 569 | } |
| 570 | void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 571 | return setSuccessor(idx, B); |
| 572 | } |
| 573 | |
Bill Wendling | 629fb82 | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 574 | bool InvokeInst::hasFnAttr(Attribute::AttrKind A) const { |
Bill Wendling | 94e94b3 | 2012-12-30 13:50:49 +0000 | [diff] [blame] | 575 | if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A)) |
Bill Wendling | 060f20a | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 576 | return true; |
| 577 | if (const Function *F = getCalledFunction()) |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 578 | return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A); |
Bill Wendling | 060f20a | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 579 | return false; |
| 580 | } |
| 581 | |
Bill Wendling | 629fb82 | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 582 | bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const { |
Bill Wendling | 94e94b3 | 2012-12-30 13:50:49 +0000 | [diff] [blame] | 583 | if (AttributeList.hasAttribute(i, A)) |
Bill Wendling | 847d165 | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 584 | return true; |
| 585 | if (const Function *F = getCalledFunction()) |
Bill Wendling | 39cd0c8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 586 | return F->getAttributes().hasAttribute(i, A); |
Bill Wendling | 365b9e0 | 2012-10-04 07:18:12 +0000 | [diff] [blame] | 587 | return false; |
| 588 | } |
| 589 | |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 590 | void InvokeInst::addAttribute(unsigned i, Attribute attr) { |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 591 | AttributeSet PAL = getAttributes(); |
Bill Wendling | c416795 | 2012-10-14 07:35:59 +0000 | [diff] [blame] | 592 | PAL = PAL.addAttr(getContext(), i, attr); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 593 | setAttributes(PAL); |
Eric Christopher | 0bf7b41 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Bill Wendling | 034b94b | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 596 | void InvokeInst::removeAttribute(unsigned i, Attribute attr) { |
Bill Wendling | 99faa3b | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 597 | AttributeSet PAL = getAttributes(); |
Bill Wendling | 5886b7b | 2012-10-14 06:39:53 +0000 | [diff] [blame] | 598 | PAL = PAL.removeAttr(getContext(), i, attr); |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 599 | setAttributes(PAL); |
Duncan Sands | f0c3354 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 602 | LandingPadInst *InvokeInst::getLandingPadInst() const { |
| 603 | return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI()); |
| 604 | } |
Duncan Sands | afa3b6d | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 605 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 606 | //===----------------------------------------------------------------------===// |
| 607 | // ReturnInst Implementation |
| 608 | //===----------------------------------------------------------------------===// |
| 609 | |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 610 | ReturnInst::ReturnInst(const ReturnInst &RI) |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 611 | : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret, |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 612 | OperandTraits<ReturnInst>::op_end(this) - |
| 613 | RI.getNumOperands(), |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 614 | RI.getNumOperands()) { |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 615 | if (RI.getNumOperands()) |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 616 | Op<0>() = RI.Op<0>(); |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 617 | SubclassOptionalData = RI.SubclassOptionalData; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 620 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore) |
| 621 | : TerminatorInst(Type::getVoidTy(C), Instruction::Ret, |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 622 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, |
| 623 | InsertBefore) { |
Devang Patel | 814ebd7 | 2008-02-26 18:49:29 +0000 | [diff] [blame] | 624 | if (retVal) |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 625 | Op<0>() = retVal; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 626 | } |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 627 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd) |
| 628 | : TerminatorInst(Type::getVoidTy(C), Instruction::Ret, |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 629 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, |
| 630 | InsertAtEnd) { |
Devang Patel | 814ebd7 | 2008-02-26 18:49:29 +0000 | [diff] [blame] | 631 | if (retVal) |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 632 | Op<0>() = retVal; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 633 | } |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 634 | ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
| 635 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret, |
Dan Gohman | fc74abf | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 636 | OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) { |
Devang Patel | 57ef4f4 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 639 | unsigned ReturnInst::getNumSuccessorsV() const { |
| 640 | return getNumSuccessors(); |
| 641 | } |
| 642 | |
Devang Patel | 64d4e61 | 2008-02-26 17:56:20 +0000 | [diff] [blame] | 643 | /// Out-of-line ReturnInst method, put here so the C++ compiler can choose to |
| 644 | /// emit the vtable for the class in this translation unit. |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 645 | void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 646 | llvm_unreachable("ReturnInst has no successors!"); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 649 | BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 650 | llvm_unreachable("ReturnInst has no successors!"); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Devang Patel | 57ef4f4 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 653 | ReturnInst::~ReturnInst() { |
Devang Patel | 57ef4f4 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 654 | } |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 655 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 656 | //===----------------------------------------------------------------------===// |
Bill Wendling | dccc03b | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 657 | // ResumeInst Implementation |
| 658 | //===----------------------------------------------------------------------===// |
| 659 | |
| 660 | ResumeInst::ResumeInst(const ResumeInst &RI) |
| 661 | : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume, |
| 662 | OperandTraits<ResumeInst>::op_begin(this), 1) { |
| 663 | Op<0>() = RI.Op<0>(); |
| 664 | } |
| 665 | |
| 666 | ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore) |
| 667 | : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume, |
| 668 | OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) { |
| 669 | Op<0>() = Exn; |
| 670 | } |
| 671 | |
| 672 | ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd) |
| 673 | : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume, |
| 674 | OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) { |
| 675 | Op<0>() = Exn; |
| 676 | } |
| 677 | |
| 678 | unsigned ResumeInst::getNumSuccessorsV() const { |
| 679 | return getNumSuccessors(); |
| 680 | } |
| 681 | |
| 682 | void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
| 683 | llvm_unreachable("ResumeInst has no successors!"); |
| 684 | } |
| 685 | |
| 686 | BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const { |
| 687 | llvm_unreachable("ResumeInst has no successors!"); |
Bill Wendling | dccc03b | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | //===----------------------------------------------------------------------===// |
Chris Lattner | b976e66 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 691 | // UnreachableInst Implementation |
| 692 | //===----------------------------------------------------------------------===// |
| 693 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 694 | UnreachableInst::UnreachableInst(LLVMContext &Context, |
| 695 | Instruction *InsertBefore) |
| 696 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable, |
| 697 | 0, 0, InsertBefore) { |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 698 | } |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 699 | UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
| 700 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable, |
| 701 | 0, 0, InsertAtEnd) { |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 704 | unsigned UnreachableInst::getNumSuccessorsV() const { |
| 705 | return getNumSuccessors(); |
| 706 | } |
| 707 | |
| 708 | void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Bill Wendling | 8833ef0 | 2012-02-06 21:44:22 +0000 | [diff] [blame] | 709 | llvm_unreachable("UnreachableInst has no successors!"); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const { |
Bill Wendling | 8833ef0 | 2012-02-06 21:44:22 +0000 | [diff] [blame] | 713 | llvm_unreachable("UnreachableInst has no successors!"); |
Chris Lattner | b976e66 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 717 | // BranchInst Implementation |
| 718 | //===----------------------------------------------------------------------===// |
| 719 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 720 | void BranchInst::AssertOK() { |
| 721 | if (isConditional()) |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 722 | assert(getCondition()->getType()->isIntegerTy(1) && |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 723 | "May only branch on boolean predicates!"); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 726 | BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore) |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 727 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 728 | OperandTraits<BranchInst>::op_end(this) - 1, |
| 729 | 1, InsertBefore) { |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 730 | assert(IfTrue != 0 && "Branch destination may not be null!"); |
Gabor Greif | ae5a20a | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 731 | Op<-1>() = IfTrue; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 732 | } |
| 733 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 734 | Instruction *InsertBefore) |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 735 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 736 | OperandTraits<BranchInst>::op_end(this) - 3, |
| 737 | 3, InsertBefore) { |
Gabor Greif | ae5a20a | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 738 | Op<-1>() = IfTrue; |
| 739 | Op<-2>() = IfFalse; |
| 740 | Op<-3>() = Cond; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 741 | #ifndef NDEBUG |
| 742 | AssertOK(); |
| 743 | #endif |
| 744 | } |
| 745 | |
| 746 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 747 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 748 | OperandTraits<BranchInst>::op_end(this) - 1, |
| 749 | 1, InsertAtEnd) { |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 750 | assert(IfTrue != 0 && "Branch destination may not be null!"); |
Gabor Greif | ae5a20a | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 751 | Op<-1>() = IfTrue; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 755 | BasicBlock *InsertAtEnd) |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 756 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 757 | OperandTraits<BranchInst>::op_end(this) - 3, |
| 758 | 3, InsertAtEnd) { |
Gabor Greif | ae5a20a | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 759 | Op<-1>() = IfTrue; |
| 760 | Op<-2>() = IfFalse; |
| 761 | Op<-3>() = Cond; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 762 | #ifndef NDEBUG |
| 763 | AssertOK(); |
| 764 | #endif |
| 765 | } |
| 766 | |
| 767 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 768 | BranchInst::BranchInst(const BranchInst &BI) : |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 769 | TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 770 | OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(), |
| 771 | BI.getNumOperands()) { |
Gabor Greif | ae5a20a | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 772 | Op<-1>() = BI.Op<-1>(); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 773 | if (BI.getNumOperands() != 1) { |
| 774 | assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); |
Gabor Greif | ae5a20a | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 775 | Op<-3>() = BI.Op<-3>(); |
| 776 | Op<-2>() = BI.Op<-2>(); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 777 | } |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 778 | SubclassOptionalData = BI.SubclassOptionalData; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Chandler Carruth | 602650c | 2011-10-17 01:11:57 +0000 | [diff] [blame] | 781 | void BranchInst::swapSuccessors() { |
| 782 | assert(isConditional() && |
| 783 | "Cannot swap successors of an unconditional branch"); |
| 784 | Op<-1>().swap(Op<-2>()); |
| 785 | |
| 786 | // Update profile metadata if present and it matches our structural |
| 787 | // expectations. |
| 788 | MDNode *ProfileData = getMetadata(LLVMContext::MD_prof); |
| 789 | if (!ProfileData || ProfileData->getNumOperands() != 3) |
| 790 | return; |
| 791 | |
| 792 | // The first operand is the name. Fetch them backwards and build a new one. |
| 793 | Value *Ops[] = { |
| 794 | ProfileData->getOperand(0), |
| 795 | ProfileData->getOperand(2), |
| 796 | ProfileData->getOperand(1) |
| 797 | }; |
| 798 | setMetadata(LLVMContext::MD_prof, |
| 799 | MDNode::get(ProfileData->getContext(), Ops)); |
| 800 | } |
| 801 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 802 | BasicBlock *BranchInst::getSuccessorV(unsigned idx) const { |
| 803 | return getSuccessor(idx); |
| 804 | } |
| 805 | unsigned BranchInst::getNumSuccessorsV() const { |
| 806 | return getNumSuccessors(); |
| 807 | } |
| 808 | void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 809 | setSuccessor(idx, B); |
| 810 | } |
| 811 | |
| 812 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 813 | //===----------------------------------------------------------------------===// |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 814 | // AllocaInst Implementation |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 815 | //===----------------------------------------------------------------------===// |
| 816 | |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 817 | static Value *getAISize(LLVMContext &Context, Value *Amt) { |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 818 | if (!Amt) |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 819 | Amt = ConstantInt::get(Type::getInt32Ty(Context), 1); |
Chris Lattner | e46749c | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 820 | else { |
| 821 | assert(!isa<BasicBlock>(Amt) && |
Chris Lattner | 53336cb | 2007-10-18 16:10:48 +0000 | [diff] [blame] | 822 | "Passed basic block into allocation size parameter! Use other ctor"); |
Dan Gohman | f75a7d3 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 823 | assert(Amt->getType()->isIntegerTy() && |
| 824 | "Allocation array size is not an integer!"); |
Chris Lattner | e46749c | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 825 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 826 | return Amt; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 829 | AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 830 | const Twine &Name, Instruction *InsertBefore) |
| 831 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
| 832 | getAISize(Ty->getContext(), ArraySize), InsertBefore) { |
| 833 | setAlignment(0); |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 834 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 835 | setName(Name); |
| 836 | } |
| 837 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 838 | AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 839 | const Twine &Name, BasicBlock *InsertAtEnd) |
| 840 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
| 841 | getAISize(Ty->getContext(), ArraySize), InsertAtEnd) { |
| 842 | setAlignment(0); |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 843 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 844 | setName(Name); |
| 845 | } |
| 846 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 847 | AllocaInst::AllocaInst(Type *Ty, const Twine &Name, |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 848 | Instruction *InsertBefore) |
| 849 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
| 850 | getAISize(Ty->getContext(), 0), InsertBefore) { |
| 851 | setAlignment(0); |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 852 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 853 | setName(Name); |
| 854 | } |
| 855 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 856 | AllocaInst::AllocaInst(Type *Ty, const Twine &Name, |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 857 | BasicBlock *InsertAtEnd) |
| 858 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
| 859 | getAISize(Ty->getContext(), 0), InsertAtEnd) { |
| 860 | setAlignment(0); |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 861 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 862 | setName(Name); |
| 863 | } |
| 864 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 865 | AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align, |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 866 | const Twine &Name, Instruction *InsertBefore) |
| 867 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
Owen Anderson | 50dead0 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 868 | getAISize(Ty->getContext(), ArraySize), InsertBefore) { |
Dan Gohman | 5283707 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 869 | setAlignment(Align); |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 870 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Chris Lattner | f00042a | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 871 | setName(Name); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 874 | AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align, |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 875 | const Twine &Name, BasicBlock *InsertAtEnd) |
| 876 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
Owen Anderson | 50dead0 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 877 | getAISize(Ty->getContext(), ArraySize), InsertAtEnd) { |
Dan Gohman | 5283707 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 878 | setAlignment(Align); |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 879 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Chris Lattner | f00042a | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 880 | setName(Name); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 883 | // Out of line virtual method, so the vtable, etc has a home. |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 884 | AllocaInst::~AllocaInst() { |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 887 | void AllocaInst::setAlignment(unsigned Align) { |
Dan Gohman | 5283707 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 888 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | 138aa2a | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 889 | assert(Align <= MaximumAlignment && |
| 890 | "Alignment is greater than MaximumAlignment!"); |
Chris Lattner | b2406d9 | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 891 | setInstructionSubclassData(Log2_32(Align) + 1); |
Dan Gohman | 5283707 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 892 | assert(getAlignment() == Align && "Alignment representation error!"); |
| 893 | } |
| 894 | |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 895 | bool AllocaInst::isArrayAllocation() const { |
Reid Spencer | bb9723b | 2007-03-01 20:27:41 +0000 | [diff] [blame] | 896 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0))) |
Dan Gohman | e0214d5 | 2010-09-27 15:15:44 +0000 | [diff] [blame] | 897 | return !CI->isOne(); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 898 | return true; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 901 | Type *AllocaInst::getAllocatedType() const { |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 902 | return getType()->getElementType(); |
| 903 | } |
| 904 | |
Chris Lattner | c5dd22a | 2008-11-26 02:54:17 +0000 | [diff] [blame] | 905 | /// isStaticAlloca - Return true if this alloca is in the entry block of the |
| 906 | /// function and is a constant size. If so, the code generator will fold it |
| 907 | /// into the prolog/epilog code, so it is basically free. |
| 908 | bool AllocaInst::isStaticAlloca() const { |
| 909 | // Must be constant size. |
| 910 | if (!isa<ConstantInt>(getArraySize())) return false; |
| 911 | |
| 912 | // Must be in the entry block. |
| 913 | const BasicBlock *Parent = getParent(); |
| 914 | return Parent == &Parent->getParent()->front(); |
| 915 | } |
| 916 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 917 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 918 | // LoadInst Implementation |
| 919 | //===----------------------------------------------------------------------===// |
| 920 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 921 | void LoadInst::AssertOK() { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 922 | assert(getOperand(0)->getType()->isPointerTy() && |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 923 | "Ptr must have pointer type."); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 924 | assert(!(isAtomic() && getAlignment() == 0) && |
| 925 | "Alignment required for atomic load"); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 926 | } |
| 927 | |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 928 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef) |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 929 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 930 | Load, Ptr, InsertBef) { |
Chris Lattner | 2866297 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 931 | setVolatile(false); |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 932 | setAlignment(0); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 933 | setAtomic(NotAtomic); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 934 | AssertOK(); |
Chris Lattner | f00042a | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 935 | setName(Name); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 938 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE) |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 939 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 940 | Load, Ptr, InsertAE) { |
Chris Lattner | 2866297 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 941 | setVolatile(false); |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 942 | setAlignment(0); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 943 | setAtomic(NotAtomic); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 944 | AssertOK(); |
Chris Lattner | f00042a | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 945 | setName(Name); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 946 | } |
| 947 | |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 948 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 949 | Instruction *InsertBef) |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 950 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 951 | Load, Ptr, InsertBef) { |
Chris Lattner | 2866297 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 952 | setVolatile(isVolatile); |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 953 | setAlignment(0); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 954 | setAtomic(NotAtomic); |
| 955 | AssertOK(); |
| 956 | setName(Name); |
| 957 | } |
| 958 | |
| 959 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
| 960 | BasicBlock *InsertAE) |
| 961 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 962 | Load, Ptr, InsertAE) { |
| 963 | setVolatile(isVolatile); |
| 964 | setAlignment(0); |
| 965 | setAtomic(NotAtomic); |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 966 | AssertOK(); |
| 967 | setName(Name); |
| 968 | } |
| 969 | |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 970 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 971 | unsigned Align, Instruction *InsertBef) |
| 972 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 973 | Load, Ptr, InsertBef) { |
| 974 | setVolatile(isVolatile); |
| 975 | setAlignment(Align); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 976 | setAtomic(NotAtomic); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 977 | AssertOK(); |
Chris Lattner | f00042a | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 978 | setName(Name); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 981 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
Dan Gohman | 6ab2d18 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 982 | unsigned Align, BasicBlock *InsertAE) |
| 983 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 984 | Load, Ptr, InsertAE) { |
| 985 | setVolatile(isVolatile); |
| 986 | setAlignment(Align); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 987 | setAtomic(NotAtomic); |
Dan Gohman | 6ab2d18 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 988 | AssertOK(); |
| 989 | setName(Name); |
| 990 | } |
| 991 | |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 992 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
| 993 | unsigned Align, AtomicOrdering Order, |
| 994 | SynchronizationScope SynchScope, |
| 995 | Instruction *InsertBef) |
| 996 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 997 | Load, Ptr, InsertBef) { |
| 998 | setVolatile(isVolatile); |
| 999 | setAlignment(Align); |
| 1000 | setAtomic(Order, SynchScope); |
| 1001 | AssertOK(); |
| 1002 | setName(Name); |
| 1003 | } |
| 1004 | |
| 1005 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
| 1006 | unsigned Align, AtomicOrdering Order, |
| 1007 | SynchronizationScope SynchScope, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1008 | BasicBlock *InsertAE) |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1009 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1010 | Load, Ptr, InsertAE) { |
Chris Lattner | f00042a | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1011 | setVolatile(isVolatile); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1012 | setAlignment(Align); |
| 1013 | setAtomic(Order, SynchScope); |
Chris Lattner | f00042a | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1014 | AssertOK(); |
| 1015 | setName(Name); |
| 1016 | } |
| 1017 | |
Daniel Dunbar | 3603d7a | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1018 | LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef) |
| 1019 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1020 | Load, Ptr, InsertBef) { |
| 1021 | setVolatile(false); |
| 1022 | setAlignment(0); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1023 | setAtomic(NotAtomic); |
Daniel Dunbar | 3603d7a | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1024 | AssertOK(); |
| 1025 | if (Name && Name[0]) setName(Name); |
| 1026 | } |
| 1027 | |
| 1028 | LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE) |
| 1029 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1030 | Load, Ptr, InsertAE) { |
| 1031 | setVolatile(false); |
| 1032 | setAlignment(0); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1033 | setAtomic(NotAtomic); |
Daniel Dunbar | 3603d7a | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1034 | AssertOK(); |
| 1035 | if (Name && Name[0]) setName(Name); |
| 1036 | } |
| 1037 | |
| 1038 | LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, |
| 1039 | Instruction *InsertBef) |
| 1040 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1041 | Load, Ptr, InsertBef) { |
| 1042 | setVolatile(isVolatile); |
| 1043 | setAlignment(0); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1044 | setAtomic(NotAtomic); |
Daniel Dunbar | 3603d7a | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1045 | AssertOK(); |
| 1046 | if (Name && Name[0]) setName(Name); |
| 1047 | } |
| 1048 | |
| 1049 | LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, |
| 1050 | BasicBlock *InsertAE) |
| 1051 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1052 | Load, Ptr, InsertAE) { |
| 1053 | setVolatile(isVolatile); |
| 1054 | setAlignment(0); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1055 | setAtomic(NotAtomic); |
Daniel Dunbar | 3603d7a | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1056 | AssertOK(); |
| 1057 | if (Name && Name[0]) setName(Name); |
| 1058 | } |
| 1059 | |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1060 | void LoadInst::setAlignment(unsigned Align) { |
| 1061 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | 138aa2a | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1062 | assert(Align <= MaximumAlignment && |
| 1063 | "Alignment is greater than MaximumAlignment!"); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1064 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) | |
Chris Lattner | b2406d9 | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 1065 | ((Log2_32(Align)+1)<<1)); |
Dan Gohman | 138aa2a | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1066 | assert(getAlignment() == Align && "Alignment representation error!"); |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1067 | } |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1068 | |
| 1069 | //===----------------------------------------------------------------------===// |
| 1070 | // StoreInst Implementation |
| 1071 | //===----------------------------------------------------------------------===// |
| 1072 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1073 | void StoreInst::AssertOK() { |
Nate Begeman | 5bc1ea0 | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 1074 | assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!"); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1075 | assert(getOperand(1)->getType()->isPointerTy() && |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1076 | "Ptr must have pointer type!"); |
| 1077 | assert(getOperand(0)->getType() == |
| 1078 | cast<PointerType>(getOperand(1)->getType())->getElementType() |
Alkis Evlogimenos | 8fabb62 | 2004-08-06 14:33:37 +0000 | [diff] [blame] | 1079 | && "Ptr must be a pointer to Val type!"); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1080 | assert(!(isAtomic() && getAlignment() == 0) && |
| 1081 | "Alignment required for atomic load"); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1082 | } |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1083 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1084 | |
| 1085 | StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1086 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1087 | OperandTraits<StoreInst>::op_begin(this), |
| 1088 | OperandTraits<StoreInst>::operands(this), |
| 1089 | InsertBefore) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1090 | Op<0>() = val; |
| 1091 | Op<1>() = addr; |
Chris Lattner | 2866297 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 1092 | setVolatile(false); |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1093 | setAlignment(0); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1094 | setAtomic(NotAtomic); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1095 | AssertOK(); |
| 1096 | } |
| 1097 | |
| 1098 | StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1099 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1100 | OperandTraits<StoreInst>::op_begin(this), |
| 1101 | OperandTraits<StoreInst>::operands(this), |
| 1102 | InsertAtEnd) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1103 | Op<0>() = val; |
| 1104 | Op<1>() = addr; |
Chris Lattner | 2866297 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 1105 | setVolatile(false); |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1106 | setAlignment(0); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1107 | setAtomic(NotAtomic); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1108 | AssertOK(); |
| 1109 | } |
| 1110 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1111 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1112 | Instruction *InsertBefore) |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1113 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1114 | OperandTraits<StoreInst>::op_begin(this), |
| 1115 | OperandTraits<StoreInst>::operands(this), |
| 1116 | InsertBefore) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1117 | Op<0>() = val; |
| 1118 | Op<1>() = addr; |
Chris Lattner | 2866297 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 1119 | setVolatile(isVolatile); |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1120 | setAlignment(0); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1121 | setAtomic(NotAtomic); |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1122 | AssertOK(); |
| 1123 | } |
| 1124 | |
| 1125 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
| 1126 | unsigned Align, Instruction *InsertBefore) |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1127 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1128 | OperandTraits<StoreInst>::op_begin(this), |
| 1129 | OperandTraits<StoreInst>::operands(this), |
| 1130 | InsertBefore) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1131 | Op<0>() = val; |
| 1132 | Op<1>() = addr; |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1133 | setVolatile(isVolatile); |
| 1134 | setAlignment(Align); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1135 | setAtomic(NotAtomic); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1136 | AssertOK(); |
| 1137 | } |
| 1138 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1139 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1140 | unsigned Align, AtomicOrdering Order, |
| 1141 | SynchronizationScope SynchScope, |
| 1142 | Instruction *InsertBefore) |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1143 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1144 | OperandTraits<StoreInst>::op_begin(this), |
| 1145 | OperandTraits<StoreInst>::operands(this), |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1146 | InsertBefore) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1147 | Op<0>() = val; |
| 1148 | Op<1>() = addr; |
Dan Gohman | 6ab2d18 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1149 | setVolatile(isVolatile); |
| 1150 | setAlignment(Align); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1151 | setAtomic(Order, SynchScope); |
Dan Gohman | 6ab2d18 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1152 | AssertOK(); |
| 1153 | } |
| 1154 | |
| 1155 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1156 | BasicBlock *InsertAtEnd) |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1157 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1158 | OperandTraits<StoreInst>::op_begin(this), |
| 1159 | OperandTraits<StoreInst>::operands(this), |
| 1160 | InsertAtEnd) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1161 | Op<0>() = val; |
| 1162 | Op<1>() = addr; |
Chris Lattner | 2866297 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 1163 | setVolatile(isVolatile); |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1164 | setAlignment(0); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1165 | setAtomic(NotAtomic); |
| 1166 | AssertOK(); |
| 1167 | } |
| 1168 | |
| 1169 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
| 1170 | unsigned Align, BasicBlock *InsertAtEnd) |
| 1171 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
| 1172 | OperandTraits<StoreInst>::op_begin(this), |
| 1173 | OperandTraits<StoreInst>::operands(this), |
| 1174 | InsertAtEnd) { |
| 1175 | Op<0>() = val; |
| 1176 | Op<1>() = addr; |
| 1177 | setVolatile(isVolatile); |
| 1178 | setAlignment(Align); |
| 1179 | setAtomic(NotAtomic); |
| 1180 | AssertOK(); |
| 1181 | } |
| 1182 | |
| 1183 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
| 1184 | unsigned Align, AtomicOrdering Order, |
| 1185 | SynchronizationScope SynchScope, |
| 1186 | BasicBlock *InsertAtEnd) |
| 1187 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
| 1188 | OperandTraits<StoreInst>::op_begin(this), |
| 1189 | OperandTraits<StoreInst>::operands(this), |
| 1190 | InsertAtEnd) { |
| 1191 | Op<0>() = val; |
| 1192 | Op<1>() = addr; |
| 1193 | setVolatile(isVolatile); |
| 1194 | setAlignment(Align); |
| 1195 | setAtomic(Order, SynchScope); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1196 | AssertOK(); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1199 | void StoreInst::setAlignment(unsigned Align) { |
| 1200 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | 138aa2a | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1201 | assert(Align <= MaximumAlignment && |
| 1202 | "Alignment is greater than MaximumAlignment!"); |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1203 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) | |
Chris Lattner | b2406d9 | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 1204 | ((Log2_32(Align)+1) << 1)); |
Dan Gohman | 138aa2a | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1205 | assert(getAlignment() == Align && "Alignment representation error!"); |
Christopher Lamb | 43c7f37 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1208 | //===----------------------------------------------------------------------===// |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1209 | // AtomicCmpXchgInst Implementation |
| 1210 | //===----------------------------------------------------------------------===// |
| 1211 | |
| 1212 | void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal, |
| 1213 | AtomicOrdering Ordering, |
| 1214 | SynchronizationScope SynchScope) { |
| 1215 | Op<0>() = Ptr; |
| 1216 | Op<1>() = Cmp; |
| 1217 | Op<2>() = NewVal; |
| 1218 | setOrdering(Ordering); |
| 1219 | setSynchScope(SynchScope); |
| 1220 | |
| 1221 | assert(getOperand(0) && getOperand(1) && getOperand(2) && |
| 1222 | "All operands must be non-null!"); |
| 1223 | assert(getOperand(0)->getType()->isPointerTy() && |
| 1224 | "Ptr must have pointer type!"); |
| 1225 | assert(getOperand(1)->getType() == |
| 1226 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1227 | && "Ptr must be a pointer to Cmp type!"); |
| 1228 | assert(getOperand(2)->getType() == |
| 1229 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1230 | && "Ptr must be a pointer to NewVal type!"); |
| 1231 | assert(Ordering != NotAtomic && |
| 1232 | "AtomicCmpXchg instructions must be atomic!"); |
| 1233 | } |
| 1234 | |
| 1235 | AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, |
| 1236 | AtomicOrdering Ordering, |
| 1237 | SynchronizationScope SynchScope, |
| 1238 | Instruction *InsertBefore) |
| 1239 | : Instruction(Cmp->getType(), AtomicCmpXchg, |
| 1240 | OperandTraits<AtomicCmpXchgInst>::op_begin(this), |
| 1241 | OperandTraits<AtomicCmpXchgInst>::operands(this), |
| 1242 | InsertBefore) { |
| 1243 | Init(Ptr, Cmp, NewVal, Ordering, SynchScope); |
| 1244 | } |
| 1245 | |
| 1246 | AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, |
| 1247 | AtomicOrdering Ordering, |
| 1248 | SynchronizationScope SynchScope, |
| 1249 | BasicBlock *InsertAtEnd) |
| 1250 | : Instruction(Cmp->getType(), AtomicCmpXchg, |
| 1251 | OperandTraits<AtomicCmpXchgInst>::op_begin(this), |
| 1252 | OperandTraits<AtomicCmpXchgInst>::operands(this), |
| 1253 | InsertAtEnd) { |
| 1254 | Init(Ptr, Cmp, NewVal, Ordering, SynchScope); |
| 1255 | } |
| 1256 | |
| 1257 | //===----------------------------------------------------------------------===// |
| 1258 | // AtomicRMWInst Implementation |
| 1259 | //===----------------------------------------------------------------------===// |
| 1260 | |
| 1261 | void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val, |
| 1262 | AtomicOrdering Ordering, |
| 1263 | SynchronizationScope SynchScope) { |
| 1264 | Op<0>() = Ptr; |
| 1265 | Op<1>() = Val; |
| 1266 | setOperation(Operation); |
| 1267 | setOrdering(Ordering); |
| 1268 | setSynchScope(SynchScope); |
| 1269 | |
| 1270 | assert(getOperand(0) && getOperand(1) && |
| 1271 | "All operands must be non-null!"); |
| 1272 | assert(getOperand(0)->getType()->isPointerTy() && |
| 1273 | "Ptr must have pointer type!"); |
| 1274 | assert(getOperand(1)->getType() == |
| 1275 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1276 | && "Ptr must be a pointer to Val type!"); |
| 1277 | assert(Ordering != NotAtomic && |
| 1278 | "AtomicRMW instructions must be atomic!"); |
| 1279 | } |
| 1280 | |
| 1281 | AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, |
| 1282 | AtomicOrdering Ordering, |
| 1283 | SynchronizationScope SynchScope, |
| 1284 | Instruction *InsertBefore) |
| 1285 | : Instruction(Val->getType(), AtomicRMW, |
| 1286 | OperandTraits<AtomicRMWInst>::op_begin(this), |
| 1287 | OperandTraits<AtomicRMWInst>::operands(this), |
| 1288 | InsertBefore) { |
| 1289 | Init(Operation, Ptr, Val, Ordering, SynchScope); |
| 1290 | } |
| 1291 | |
| 1292 | AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, |
| 1293 | AtomicOrdering Ordering, |
| 1294 | SynchronizationScope SynchScope, |
| 1295 | BasicBlock *InsertAtEnd) |
| 1296 | : Instruction(Val->getType(), AtomicRMW, |
| 1297 | OperandTraits<AtomicRMWInst>::op_begin(this), |
| 1298 | OperandTraits<AtomicRMWInst>::operands(this), |
| 1299 | InsertAtEnd) { |
| 1300 | Init(Operation, Ptr, Val, Ordering, SynchScope); |
| 1301 | } |
| 1302 | |
| 1303 | //===----------------------------------------------------------------------===// |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1304 | // FenceInst Implementation |
| 1305 | //===----------------------------------------------------------------------===// |
| 1306 | |
| 1307 | FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, |
| 1308 | SynchronizationScope SynchScope, |
| 1309 | Instruction *InsertBefore) |
| 1310 | : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertBefore) { |
| 1311 | setOrdering(Ordering); |
| 1312 | setSynchScope(SynchScope); |
| 1313 | } |
| 1314 | |
| 1315 | FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, |
| 1316 | SynchronizationScope SynchScope, |
| 1317 | BasicBlock *InsertAtEnd) |
| 1318 | : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertAtEnd) { |
| 1319 | setOrdering(Ordering); |
| 1320 | setSynchScope(SynchScope); |
| 1321 | } |
| 1322 | |
| 1323 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1324 | // GetElementPtrInst Implementation |
| 1325 | //===----------------------------------------------------------------------===// |
| 1326 | |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1327 | void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1328 | const Twine &Name) { |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1329 | assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?"); |
| 1330 | OperandList[0] = Ptr; |
| 1331 | std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1); |
Matthijs Kooijman | 338169d | 2008-06-04 16:14:12 +0000 | [diff] [blame] | 1332 | setName(Name); |
Chris Lattner | 38bacf2 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1335 | GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI) |
Gabor Greif | b9d9eba | 2008-05-27 11:03:29 +0000 | [diff] [blame] | 1336 | : Instruction(GEPI.getType(), GetElementPtr, |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 1337 | OperandTraits<GetElementPtrInst>::op_end(this) |
| 1338 | - GEPI.getNumOperands(), |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1339 | GEPI.getNumOperands()) { |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1340 | std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin()); |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1341 | SubclassOptionalData = GEPI.SubclassOptionalData; |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
Chris Lattner | c66996a | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1344 | /// getIndexedType - Returns the type of the element that would be accessed with |
| 1345 | /// a gep instruction with the specified parameters. |
| 1346 | /// |
| 1347 | /// The Idxs pointer should point to a continuous piece of memory containing the |
| 1348 | /// indices, either as Value* or uint64_t. |
| 1349 | /// |
| 1350 | /// A null type is returned if the indices are invalid for the specified |
| 1351 | /// pointer type. |
| 1352 | /// |
Matthijs Kooijman | e2afded | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1353 | template <typename IndexTy> |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1354 | static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) { |
Duncan Sands | 2333e29 | 2012-11-13 12:59:33 +0000 | [diff] [blame] | 1355 | PointerType *PTy = dyn_cast<PointerType>(Ptr->getScalarType()); |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1356 | if (!PTy) return 0; // Type isn't a pointer type! |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1357 | Type *Agg = PTy->getElementType(); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1358 | |
Chris Lattner | c66996a | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1359 | // Handle the special case of the empty set index set, which is always valid. |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1360 | if (IdxList.empty()) |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1361 | return Agg; |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1362 | |
Chris Lattner | c66996a | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1363 | // If there is at least one index, the top level type must be sized, otherwise |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1364 | // it cannot be 'stepped over'. |
| 1365 | if (!Agg->isSized()) |
Chris Lattner | c66996a | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1366 | return 0; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1367 | |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1368 | unsigned CurIdx = 1; |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1369 | for (; CurIdx != IdxList.size(); ++CurIdx) { |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1370 | CompositeType *CT = dyn_cast<CompositeType>(Agg); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1371 | if (!CT || CT->isPointerTy()) return 0; |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1372 | IndexTy Index = IdxList[CurIdx]; |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1373 | if (!CT->indexValid(Index)) return 0; |
| 1374 | Agg = CT->getTypeAtIndex(Index); |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1375 | } |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1376 | return CurIdx == IdxList.size() ? Agg : 0; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1379 | Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList) { |
| 1380 | return getIndexedTypeInternal(Ptr, IdxList); |
Matthijs Kooijman | e2afded | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1383 | Type *GetElementPtrInst::getIndexedType(Type *Ptr, |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1384 | ArrayRef<Constant *> IdxList) { |
| 1385 | return getIndexedTypeInternal(Ptr, IdxList); |
Jay Foad | 25052d8 | 2011-01-14 08:07:43 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
Jay Foad | a920310 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1388 | Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) { |
| 1389 | return getIndexedTypeInternal(Ptr, IdxList); |
Matthijs Kooijman | e2afded | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
Chris Lattner | 6f771d4 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1392 | /// hasAllZeroIndices - Return true if all of the indices of this GEP are |
| 1393 | /// zeros. If so, the result pointer and the first operand have the same |
| 1394 | /// value, just potentially different types. |
| 1395 | bool GetElementPtrInst::hasAllZeroIndices() const { |
| 1396 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1397 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) { |
| 1398 | if (!CI->isZero()) return false; |
| 1399 | } else { |
| 1400 | return false; |
| 1401 | } |
| 1402 | } |
| 1403 | return true; |
| 1404 | } |
| 1405 | |
Chris Lattner | 6b0974c | 2007-04-27 20:35:56 +0000 | [diff] [blame] | 1406 | /// hasAllConstantIndices - Return true if all of the indices of this GEP are |
| 1407 | /// constant integers. If so, the result pointer and the first operand have |
| 1408 | /// a constant offset between them. |
| 1409 | bool GetElementPtrInst::hasAllConstantIndices() const { |
| 1410 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1411 | if (!isa<ConstantInt>(getOperand(i))) |
| 1412 | return false; |
| 1413 | } |
| 1414 | return true; |
| 1415 | } |
| 1416 | |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1417 | void GetElementPtrInst::setIsInBounds(bool B) { |
| 1418 | cast<GEPOperator>(this)->setIsInBounds(B); |
| 1419 | } |
Chris Lattner | 6f771d4 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1420 | |
Nick Lewycky | ae05e7d | 2009-09-27 21:33:04 +0000 | [diff] [blame] | 1421 | bool GetElementPtrInst::isInBounds() const { |
| 1422 | return cast<GEPOperator>(this)->isInBounds(); |
| 1423 | } |
| 1424 | |
Chandler Carruth | 4ced4ee | 2012-12-11 10:29:10 +0000 | [diff] [blame] | 1425 | bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL, |
| 1426 | APInt &Offset) const { |
Chandler Carruth | 7550f96 | 2012-12-11 11:05:15 +0000 | [diff] [blame] | 1427 | // Delegate to the generic GEPOperator implementation. |
| 1428 | return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset); |
Chandler Carruth | 4ced4ee | 2012-12-11 10:29:10 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1431 | //===----------------------------------------------------------------------===// |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1432 | // ExtractElementInst Implementation |
| 1433 | //===----------------------------------------------------------------------===// |
| 1434 | |
| 1435 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1436 | const Twine &Name, |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1437 | Instruction *InsertBef) |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1438 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1439 | ExtractElement, |
| 1440 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1441 | 2, InsertBef) { |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1442 | assert(isValidOperands(Val, Index) && |
| 1443 | "Invalid extractelement instruction operands!"); |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1444 | Op<0>() = Val; |
| 1445 | Op<1>() = Index; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1446 | setName(Name); |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1450 | const Twine &Name, |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1451 | BasicBlock *InsertAE) |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1452 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1453 | ExtractElement, |
| 1454 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1455 | 2, InsertAE) { |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1456 | assert(isValidOperands(Val, Index) && |
| 1457 | "Invalid extractelement instruction operands!"); |
| 1458 | |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1459 | Op<0>() = Val; |
| 1460 | Op<1>() = Index; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1461 | setName(Name); |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Chris Lattner | 06a248c2 | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1464 | |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1465 | bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1466 | if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32)) |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1467 | return false; |
| 1468 | return true; |
| 1469 | } |
| 1470 | |
| 1471 | |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1472 | //===----------------------------------------------------------------------===// |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1473 | // InsertElementInst Implementation |
| 1474 | //===----------------------------------------------------------------------===// |
| 1475 | |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1476 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1477 | const Twine &Name, |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1478 | Instruction *InsertBef) |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1479 | : Instruction(Vec->getType(), InsertElement, |
| 1480 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1481 | 3, InsertBef) { |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1482 | assert(isValidOperands(Vec, Elt, Index) && |
| 1483 | "Invalid insertelement instruction operands!"); |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1484 | Op<0>() = Vec; |
| 1485 | Op<1>() = Elt; |
| 1486 | Op<2>() = Index; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1487 | setName(Name); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1490 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1491 | const Twine &Name, |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1492 | BasicBlock *InsertAE) |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1493 | : Instruction(Vec->getType(), InsertElement, |
| 1494 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1495 | 3, InsertAE) { |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1496 | assert(isValidOperands(Vec, Elt, Index) && |
| 1497 | "Invalid insertelement instruction operands!"); |
| 1498 | |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1499 | Op<0>() = Vec; |
| 1500 | Op<1>() = Elt; |
| 1501 | Op<2>() = Index; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1502 | setName(Name); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1505 | bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, |
| 1506 | const Value *Index) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1507 | if (!Vec->getType()->isVectorTy()) |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1508 | return false; // First operand of insertelement must be vector type. |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1509 | |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1510 | if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType()) |
Dan Gohman | 86296cc | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1511 | return false;// Second operand of insertelement must be vector element type. |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1512 | |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1513 | if (!Index->getType()->isIntegerTy(32)) |
Dan Gohman | a119de8 | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 1514 | return false; // Third operand of insertelement must be i32. |
Chris Lattner | d2325d0 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1515 | return true; |
| 1516 | } |
| 1517 | |
| 1518 | |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1519 | //===----------------------------------------------------------------------===// |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1520 | // ShuffleVectorInst Implementation |
| 1521 | //===----------------------------------------------------------------------===// |
| 1522 | |
| 1523 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1524 | const Twine &Name, |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1525 | Instruction *InsertBefore) |
Owen Anderson | debcb01 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1526 | : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1527 | cast<VectorType>(Mask->getType())->getNumElements()), |
| 1528 | ShuffleVector, |
| 1529 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1530 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1531 | InsertBefore) { |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1532 | assert(isValidOperands(V1, V2, Mask) && |
| 1533 | "Invalid shuffle vector instruction operands!"); |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1534 | Op<0>() = V1; |
| 1535 | Op<1>() = V2; |
| 1536 | Op<2>() = Mask; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1537 | setName(Name); |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1541 | const Twine &Name, |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1542 | BasicBlock *InsertAtEnd) |
Dan Gohman | 638d853 | 2009-08-25 23:27:45 +0000 | [diff] [blame] | 1543 | : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), |
| 1544 | cast<VectorType>(Mask->getType())->getNumElements()), |
| 1545 | ShuffleVector, |
| 1546 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1547 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1548 | InsertAtEnd) { |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1549 | assert(isValidOperands(V1, V2, Mask) && |
| 1550 | "Invalid shuffle vector instruction operands!"); |
| 1551 | |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1552 | Op<0>() = V1; |
| 1553 | Op<1>() = V2; |
| 1554 | Op<2>() = Mask; |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1555 | setName(Name); |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
Mon P Wang | aeb06d2 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1558 | bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1559 | const Value *Mask) { |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1560 | // V1 and V2 must be vectors of the same type. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1561 | if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType()) |
Chris Lattner | 8728f19 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1562 | return false; |
| 1563 | |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1564 | // Mask must be vector of i32. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1565 | VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType()); |
Nate Begeman | a966af2 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1566 | if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32)) |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1567 | return false; |
Nate Begeman | a966af2 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1568 | |
| 1569 | // Check to see if Mask is valid. |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1570 | if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask)) |
| 1571 | return true; |
| 1572 | |
Nate Begeman | a966af2 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1573 | if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) { |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1574 | unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements(); |
Nate Begeman | a966af2 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1575 | for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) { |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1576 | if (ConstantInt *CI = dyn_cast<ConstantInt>(MV->getOperand(i))) { |
| 1577 | if (CI->uge(V1Size*2)) |
Nate Begeman | a966af2 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1578 | return false; |
| 1579 | } else if (!isa<UndefValue>(MV->getOperand(i))) { |
| 1580 | return false; |
| 1581 | } |
| 1582 | } |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1583 | return true; |
Mon P Wang | cf62b37 | 2011-10-26 00:34:48 +0000 | [diff] [blame] | 1584 | } |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1585 | |
| 1586 | if (const ConstantDataSequential *CDS = |
| 1587 | dyn_cast<ConstantDataSequential>(Mask)) { |
| 1588 | unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements(); |
| 1589 | for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i) |
| 1590 | if (CDS->getElementAsInteger(i) >= V1Size*2) |
| 1591 | return false; |
| 1592 | return true; |
| 1593 | } |
| 1594 | |
| 1595 | // The bitcode reader can create a place holder for a forward reference |
| 1596 | // used as the shuffle mask. When this occurs, the shuffle mask will |
| 1597 | // fall into this case and fail. To avoid this error, do this bit of |
| 1598 | // ugliness to allow such a mask pass. |
| 1599 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask)) |
| 1600 | if (CE->getOpcode() == Instruction::UserOp1) |
| 1601 | return true; |
| 1602 | |
| 1603 | return false; |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Chris Lattner | 8728f19 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1606 | /// getMaskValue - Return the index from the shuffle mask for the specified |
| 1607 | /// output result. This is either -1 if the element is undef or a number less |
| 1608 | /// than 2*numelements. |
Chris Lattner | 56243b8 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1609 | int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) { |
| 1610 | assert(i < Mask->getType()->getVectorNumElements() && "Index out of range"); |
| 1611 | if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask)) |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1612 | return CDS->getElementAsInteger(i); |
Chris Lattner | 56243b8 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1613 | Constant *C = Mask->getAggregateElement(i); |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1614 | if (isa<UndefValue>(C)) |
Chris Lattner | 8728f19 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1615 | return -1; |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1616 | return cast<ConstantInt>(C)->getZExtValue(); |
Chris Lattner | 8728f19 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1619 | /// getShuffleMask - Return the full mask for this instruction, where each |
| 1620 | /// element is the element number and undef's are returned as -1. |
Chris Lattner | 56243b8 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1621 | void ShuffleVectorInst::getShuffleMask(Constant *Mask, |
| 1622 | SmallVectorImpl<int> &Result) { |
| 1623 | unsigned NumElts = Mask->getType()->getVectorNumElements(); |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1624 | |
Chris Lattner | 56243b8 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1625 | if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) { |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1626 | for (unsigned i = 0; i != NumElts; ++i) |
| 1627 | Result.push_back(CDS->getElementAsInteger(i)); |
| 1628 | return; |
| 1629 | } |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1630 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1631 | Constant *C = Mask->getAggregateElement(i); |
| 1632 | Result.push_back(isa<UndefValue>(C) ? -1 : |
Chris Lattner | 220dfa7 | 2012-01-26 00:41:50 +0000 | [diff] [blame] | 1633 | cast<ConstantInt>(C)->getZExtValue()); |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1638 | //===----------------------------------------------------------------------===// |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1639 | // InsertValueInst Class |
| 1640 | //===----------------------------------------------------------------------===// |
| 1641 | |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1642 | void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, |
| 1643 | const Twine &Name) { |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1644 | assert(NumOperands == 2 && "NumOperands not initialized?"); |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1645 | |
| 1646 | // There's no fundamental reason why we require at least one index |
| 1647 | // (other than weirdness with &*IdxBegin being invalid; see |
| 1648 | // getelementptr's init routine for example). But there's no |
| 1649 | // present need to support it. |
| 1650 | assert(Idxs.size() > 0 && "InsertValueInst must have at least one index"); |
| 1651 | |
| 1652 | assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) == |
Frits van Bommel | a4805cf | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1653 | Val->getType() && "Inserted value must match indexed type!"); |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1654 | Op<0>() = Agg; |
| 1655 | Op<1>() = Val; |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1656 | |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1657 | Indices.append(Idxs.begin(), Idxs.end()); |
Matthijs Kooijman | 444099f6 | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1658 | setName(Name); |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | InsertValueInst::InsertValueInst(const InsertValueInst &IVI) |
Gabor Greif | b9d9eba | 2008-05-27 11:03:29 +0000 | [diff] [blame] | 1662 | : Instruction(IVI.getType(), InsertValue, |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1663 | OperandTraits<InsertValueInst>::op_begin(this), 2), |
| 1664 | Indices(IVI.Indices) { |
Dan Gohman | 80b9626 | 2008-06-17 23:25:49 +0000 | [diff] [blame] | 1665 | Op<0>() = IVI.getOperand(0); |
| 1666 | Op<1>() = IVI.getOperand(1); |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1667 | SubclassOptionalData = IVI.SubclassOptionalData; |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | //===----------------------------------------------------------------------===// |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1671 | // ExtractValueInst Class |
| 1672 | //===----------------------------------------------------------------------===// |
| 1673 | |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1674 | void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) { |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1675 | assert(NumOperands == 1 && "NumOperands not initialized?"); |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1676 | |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1677 | // There's no fundamental reason why we require at least one index. |
| 1678 | // But there's no present need to support it. |
| 1679 | assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index"); |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1680 | |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1681 | Indices.append(Idxs.begin(), Idxs.end()); |
Matthijs Kooijman | 444099f6 | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1682 | setName(Name); |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI) |
Gabor Greif | d4f268b | 2008-06-06 20:28:12 +0000 | [diff] [blame] | 1686 | : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)), |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1687 | Indices(EVI.Indices) { |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1688 | SubclassOptionalData = EVI.SubclassOptionalData; |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1691 | // getIndexedType - Returns the type of the element that would be extracted |
| 1692 | // with an extractvalue instruction with the specified parameters. |
| 1693 | // |
| 1694 | // A null type is returned if the indices are invalid for the specified |
| 1695 | // pointer type. |
| 1696 | // |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1697 | Type *ExtractValueInst::getIndexedType(Type *Agg, |
Jay Foad | fc6d3a4 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1698 | ArrayRef<unsigned> Idxs) { |
| 1699 | for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) { |
Dan Gohman | 81a0c0b | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1700 | unsigned Index = Idxs[CurIdx]; |
Frits van Bommel | a4805cf | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1701 | // We can't use CompositeType::indexValid(Index) here. |
| 1702 | // indexValid() always returns true for arrays because getelementptr allows |
| 1703 | // out-of-bounds indices. Since we don't allow those for extractvalue and |
| 1704 | // insertvalue we need to check array indexing manually. |
| 1705 | // Since the only other types we can index into are struct types it's just |
| 1706 | // as easy to check those manually as well. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1707 | if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) { |
Frits van Bommel | a4805cf | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1708 | if (Index >= AT->getNumElements()) |
| 1709 | return 0; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1710 | } else if (StructType *ST = dyn_cast<StructType>(Agg)) { |
Frits van Bommel | a4805cf | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1711 | if (Index >= ST->getNumElements()) |
| 1712 | return 0; |
| 1713 | } else { |
| 1714 | // Not a valid type to index into. |
| 1715 | return 0; |
| 1716 | } |
| 1717 | |
| 1718 | Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index); |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1719 | } |
Chris Lattner | 1afcace | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1720 | return const_cast<Type*>(Agg); |
Dan Gohman | 041e2eb | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1721 | } |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1722 | |
| 1723 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1724 | // BinaryOperator Class |
| 1725 | //===----------------------------------------------------------------------===// |
| 1726 | |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1727 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1728 | Type *Ty, const Twine &Name, |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1729 | Instruction *InsertBefore) |
Dan Gohman | 1eaac53 | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 1730 | : Instruction(Ty, iType, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1731 | OperandTraits<BinaryOperator>::op_begin(this), |
| 1732 | OperandTraits<BinaryOperator>::operands(this), |
| 1733 | InsertBefore) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1734 | Op<0>() = S1; |
| 1735 | Op<1>() = S2; |
Dan Gohman | 1eaac53 | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 1736 | init(iType); |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1737 | setName(Name); |
| 1738 | } |
| 1739 | |
| 1740 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1741 | Type *Ty, const Twine &Name, |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1742 | BasicBlock *InsertAtEnd) |
Dan Gohman | 1eaac53 | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 1743 | : Instruction(Ty, iType, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1744 | OperandTraits<BinaryOperator>::op_begin(this), |
| 1745 | OperandTraits<BinaryOperator>::operands(this), |
| 1746 | InsertAtEnd) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1747 | Op<0>() = S1; |
| 1748 | Op<1>() = S2; |
Dan Gohman | 1eaac53 | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 1749 | init(iType); |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1750 | setName(Name); |
| 1751 | } |
| 1752 | |
| 1753 | |
| 1754 | void BinaryOperator::init(BinaryOps iType) { |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1755 | Value *LHS = getOperand(0), *RHS = getOperand(1); |
Jeffrey Yasskin | 8e68c38 | 2010-12-23 00:58:24 +0000 | [diff] [blame] | 1756 | (void)LHS; (void)RHS; // Silence warnings. |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1757 | assert(LHS->getType() == RHS->getType() && |
| 1758 | "Binary operator operand types must match!"); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1759 | #ifndef NDEBUG |
| 1760 | switch (iType) { |
| 1761 | case Add: case Sub: |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1762 | case Mul: |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1763 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1764 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1765 | assert(getType()->isIntOrIntVectorTy() && |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1766 | "Tried to create an integer operation on a non-integer type!"); |
| 1767 | break; |
| 1768 | case FAdd: case FSub: |
| 1769 | case FMul: |
| 1770 | assert(getType() == LHS->getType() && |
| 1771 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1772 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1773 | "Tried to create a floating-point operation on a " |
| 1774 | "non-floating-point type!"); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1775 | break; |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1776 | case UDiv: |
| 1777 | case SDiv: |
| 1778 | assert(getType() == LHS->getType() && |
| 1779 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1780 | assert((getType()->isIntegerTy() || (getType()->isVectorTy() && |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1781 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1782 | "Incorrect operand type (not integer) for S/UDIV"); |
| 1783 | break; |
| 1784 | case FDiv: |
| 1785 | assert(getType() == LHS->getType() && |
| 1786 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1787 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | f57478f | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1788 | "Incorrect operand type (not floating point) for FDIV"); |
Reid Spencer | 1628cec | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1789 | break; |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1790 | case URem: |
| 1791 | case SRem: |
| 1792 | assert(getType() == LHS->getType() && |
| 1793 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1794 | assert((getType()->isIntegerTy() || (getType()->isVectorTy() && |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1795 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1796 | "Incorrect operand type (not integer) for S/UREM"); |
| 1797 | break; |
| 1798 | case FRem: |
| 1799 | assert(getType() == LHS->getType() && |
| 1800 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1801 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | f57478f | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1802 | "Incorrect operand type (not floating point) for FREM"); |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1803 | break; |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1804 | case Shl: |
| 1805 | case LShr: |
| 1806 | case AShr: |
| 1807 | assert(getType() == LHS->getType() && |
| 1808 | "Shift operation should return same type as operands!"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1809 | assert((getType()->isIntegerTy() || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1810 | (getType()->isVectorTy() && |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1811 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Nate Begeman | 5bc1ea0 | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 1812 | "Tried to create a shift operation on a non-integral type!"); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1813 | break; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1814 | case And: case Or: |
| 1815 | case Xor: |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1816 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1817 | "Logical operation should return same type as operands!"); |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1818 | assert((getType()->isIntegerTy() || |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1819 | (getType()->isVectorTy() && |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1820 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Misha Brukman | 1bae291 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 1821 | "Tried to create a logical operation on a non-integral type!"); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1822 | break; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1823 | default: |
| 1824 | break; |
| 1825 | } |
| 1826 | #endif |
| 1827 | } |
| 1828 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1829 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1830 | const Twine &Name, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1831 | Instruction *InsertBefore) { |
| 1832 | assert(S1->getType() == S2->getType() && |
| 1833 | "Cannot create binary operator with two operands of differing type!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1834 | return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1837 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1838 | const Twine &Name, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1839 | BasicBlock *InsertAtEnd) { |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1840 | BinaryOperator *Res = Create(Op, S1, S2, Name); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1841 | InsertAtEnd->getInstList().push_back(Res); |
| 1842 | return Res; |
| 1843 | } |
| 1844 | |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1845 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1846 | Instruction *InsertBefore) { |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1847 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1848 | return new BinaryOperator(Instruction::Sub, |
| 1849 | zero, Op, |
| 1850 | Op->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1853 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1854 | BasicBlock *InsertAtEnd) { |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1855 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1856 | return new BinaryOperator(Instruction::Sub, |
| 1857 | zero, Op, |
| 1858 | Op->getType(), Name, InsertAtEnd); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
Dan Gohman | bdc46c6 | 2009-12-18 02:58:50 +0000 | [diff] [blame] | 1861 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, |
| 1862 | Instruction *InsertBefore) { |
| 1863 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 1864 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore); |
| 1865 | } |
| 1866 | |
| 1867 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, |
| 1868 | BasicBlock *InsertAtEnd) { |
| 1869 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 1870 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd); |
| 1871 | } |
| 1872 | |
Duncan Sands | 8991d51 | 2010-02-02 12:53:04 +0000 | [diff] [blame] | 1873 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, |
| 1874 | Instruction *InsertBefore) { |
| 1875 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 1876 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore); |
| 1877 | } |
| 1878 | |
| 1879 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, |
| 1880 | BasicBlock *InsertAtEnd) { |
| 1881 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 1882 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd); |
| 1883 | } |
| 1884 | |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1885 | BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1886 | Instruction *InsertBefore) { |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1887 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Chris Lattner | 4ca829e | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 1888 | return new BinaryOperator(Instruction::FSub, zero, Op, |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1889 | Op->getType(), Name, InsertBefore); |
| 1890 | } |
| 1891 | |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1892 | BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1893 | BasicBlock *InsertAtEnd) { |
Owen Anderson | 6f83c9c | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1894 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Chris Lattner | 4ca829e | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 1895 | return new BinaryOperator(Instruction::FSub, zero, Op, |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1896 | Op->getType(), Name, InsertAtEnd); |
| 1897 | } |
| 1898 | |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1899 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1900 | Instruction *InsertBefore) { |
Chris Lattner | 4ca829e | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 1901 | Constant *C = Constant::getAllOnesValue(Op->getType()); |
Chris Lattner | fdbc82a | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1902 | return new BinaryOperator(Instruction::Xor, Op, C, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1903 | Op->getType(), Name, InsertBefore); |
| 1904 | } |
| 1905 | |
Dan Gohman | 4ae5126 | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1906 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1907 | BasicBlock *InsertAtEnd) { |
Chris Lattner | 4ca829e | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 1908 | Constant *AllOnes = Constant::getAllOnesValue(Op->getType()); |
Chris Lattner | b9d4100 | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1909 | return new BinaryOperator(Instruction::Xor, Op, AllOnes, |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1910 | Op->getType(), Name, InsertAtEnd); |
| 1911 | } |
| 1912 | |
| 1913 | |
| 1914 | // isConstantAllOnes - Helper function for several functions below |
| 1915 | static inline bool isConstantAllOnes(const Value *V) { |
Chris Lattner | a78fa8c | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 1916 | if (const Constant *C = dyn_cast<Constant>(V)) |
| 1917 | return C->isAllOnesValue(); |
Chris Lattner | e20b7be | 2007-06-15 06:04:24 +0000 | [diff] [blame] | 1918 | return false; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
Owen Anderson | fa82b6e | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 1921 | bool BinaryOperator::isNeg(const Value *V) { |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1922 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1923 | if (Bop->getOpcode() == Instruction::Sub) |
Owen Anderson | fa82b6e | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 1924 | if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) |
| 1925 | return C->isNegativeZeroValue(); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1926 | return false; |
| 1927 | } |
| 1928 | |
Shuxin Yang | 935e35d | 2013-01-09 00:13:41 +0000 | [diff] [blame^] | 1929 | bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) { |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1930 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1931 | if (Bop->getOpcode() == Instruction::FSub) |
Shuxin Yang | 935e35d | 2013-01-09 00:13:41 +0000 | [diff] [blame^] | 1932 | if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) { |
| 1933 | if (!IgnoreZeroSign) |
| 1934 | IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros(); |
| 1935 | return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue(); |
| 1936 | } |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1937 | return false; |
| 1938 | } |
| 1939 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1940 | bool BinaryOperator::isNot(const Value *V) { |
| 1941 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1942 | return (Bop->getOpcode() == Instruction::Xor && |
| 1943 | (isConstantAllOnes(Bop->getOperand(1)) || |
| 1944 | isConstantAllOnes(Bop->getOperand(0)))); |
| 1945 | return false; |
| 1946 | } |
| 1947 | |
Chris Lattner | 64001d0 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1948 | Value *BinaryOperator::getNegArgument(Value *BinOp) { |
Chris Lattner | 64001d0 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1949 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1950 | } |
| 1951 | |
Chris Lattner | 64001d0 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1952 | const Value *BinaryOperator::getNegArgument(const Value *BinOp) { |
| 1953 | return getNegArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1956 | Value *BinaryOperator::getFNegArgument(Value *BinOp) { |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1957 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
| 1958 | } |
| 1959 | |
| 1960 | const Value *BinaryOperator::getFNegArgument(const Value *BinOp) { |
| 1961 | return getFNegArgument(const_cast<Value*>(BinOp)); |
| 1962 | } |
| 1963 | |
Chris Lattner | 64001d0 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1964 | Value *BinaryOperator::getNotArgument(Value *BinOp) { |
| 1965 | assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!"); |
| 1966 | BinaryOperator *BO = cast<BinaryOperator>(BinOp); |
| 1967 | Value *Op0 = BO->getOperand(0); |
| 1968 | Value *Op1 = BO->getOperand(1); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1969 | if (isConstantAllOnes(Op0)) return Op1; |
| 1970 | |
| 1971 | assert(isConstantAllOnes(Op1)); |
| 1972 | return Op0; |
| 1973 | } |
| 1974 | |
Chris Lattner | 64001d0 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1975 | const Value *BinaryOperator::getNotArgument(const Value *BinOp) { |
| 1976 | return getNotArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | |
| 1980 | // swapOperands - Exchange the two operands to this instruction. This |
| 1981 | // instruction is safe to use on any binary instruction and does not |
| 1982 | // modify the semantics of the instruction. If the instruction is |
| 1983 | // order dependent (SetLT f.e.) the opcode is changed. |
| 1984 | // |
| 1985 | bool BinaryOperator::swapOperands() { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1986 | if (!isCommutative()) |
| 1987 | return true; // Can't commute operands |
Gabor Greif | 94fb68b | 2008-05-13 22:51:52 +0000 | [diff] [blame] | 1988 | Op<0>().swap(Op<1>()); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1989 | return false; |
| 1990 | } |
| 1991 | |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1992 | void BinaryOperator::setHasNoUnsignedWrap(bool b) { |
| 1993 | cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b); |
| 1994 | } |
| 1995 | |
| 1996 | void BinaryOperator::setHasNoSignedWrap(bool b) { |
| 1997 | cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b); |
| 1998 | } |
| 1999 | |
| 2000 | void BinaryOperator::setIsExact(bool b) { |
Chris Lattner | 35bda89 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 2001 | cast<PossiblyExactOperator>(this)->setIsExact(b); |
Dan Gohman | f8dbee7 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2002 | } |
| 2003 | |
Nick Lewycky | ae05e7d | 2009-09-27 21:33:04 +0000 | [diff] [blame] | 2004 | bool BinaryOperator::hasNoUnsignedWrap() const { |
| 2005 | return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap(); |
| 2006 | } |
| 2007 | |
| 2008 | bool BinaryOperator::hasNoSignedWrap() const { |
| 2009 | return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap(); |
| 2010 | } |
| 2011 | |
| 2012 | bool BinaryOperator::isExact() const { |
Chris Lattner | 35bda89 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 2013 | return cast<PossiblyExactOperator>(this)->isExact(); |
Nick Lewycky | ae05e7d | 2009-09-27 21:33:04 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
Chris Lattner | 79bc332 | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2016 | //===----------------------------------------------------------------------===// |
Duncan Sands | 8883c43 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2017 | // FPMathOperator Class |
| 2018 | //===----------------------------------------------------------------------===// |
| 2019 | |
| 2020 | /// getFPAccuracy - Get the maximum error permitted by this operation in ULPs. |
| 2021 | /// An accuracy of 0.0 means that the operation should be performed with the |
Duncan Sands | 2867c85 | 2012-04-16 19:39:33 +0000 | [diff] [blame] | 2022 | /// default precision. |
Duncan Sands | 8883c43 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2023 | float FPMathOperator::getFPAccuracy() const { |
| 2024 | const MDNode *MD = |
| 2025 | cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath); |
| 2026 | if (!MD) |
| 2027 | return 0.0; |
Duncan Sands | 2867c85 | 2012-04-16 19:39:33 +0000 | [diff] [blame] | 2028 | ConstantFP *Accuracy = cast<ConstantFP>(MD->getOperand(0)); |
| 2029 | return Accuracy->getValueAPF().convertToFloat(); |
Duncan Sands | 8883c43 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | |
| 2033 | //===----------------------------------------------------------------------===// |
Chris Lattner | 79bc332 | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2034 | // CastInst Class |
| 2035 | //===----------------------------------------------------------------------===// |
| 2036 | |
David Blaikie | 0becc96 | 2011-12-01 08:00:17 +0000 | [diff] [blame] | 2037 | void CastInst::anchor() {} |
| 2038 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2039 | // Just determine if this cast only deals with integral->integral conversion. |
| 2040 | bool CastInst::isIntegerCast() const { |
| 2041 | switch (getOpcode()) { |
| 2042 | default: return false; |
| 2043 | case Instruction::ZExt: |
| 2044 | case Instruction::SExt: |
| 2045 | case Instruction::Trunc: |
| 2046 | return true; |
| 2047 | case Instruction::BitCast: |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2048 | return getOperand(0)->getType()->isIntegerTy() && |
| 2049 | getType()->isIntegerTy(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2050 | } |
Chris Lattner | 79bc332 | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2053 | bool CastInst::isLosslessCast() const { |
| 2054 | // Only BitCast can be lossless, exit fast if we're not BitCast |
| 2055 | if (getOpcode() != Instruction::BitCast) |
| 2056 | return false; |
| 2057 | |
| 2058 | // Identity cast is always lossless |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2059 | Type* SrcTy = getOperand(0)->getType(); |
| 2060 | Type* DstTy = getType(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2061 | if (SrcTy == DstTy) |
| 2062 | return true; |
| 2063 | |
Reid Spencer | 79e21d3 | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 2064 | // Pointer to pointer is always lossless. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2065 | if (SrcTy->isPointerTy()) |
| 2066 | return DstTy->isPointerTy(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2067 | return false; // Other types have no identity values |
| 2068 | } |
| 2069 | |
| 2070 | /// This function determines if the CastInst does not require any bits to be |
| 2071 | /// changed in order to effect the cast. Essentially, it identifies cases where |
| 2072 | /// no code gen is necessary for the cast, hence the name no-op cast. For |
| 2073 | /// example, the following are all no-op casts: |
Dan Gohman | 9b38d7d | 2008-05-12 16:34:30 +0000 | [diff] [blame] | 2074 | /// # bitcast i32* %x to i8* |
| 2075 | /// # bitcast <2 x i32> %x to <4 x i16> |
| 2076 | /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only |
Dan Gohman | 2c048ea | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2077 | /// @brief Determine if the described cast is a no-op. |
| 2078 | bool CastInst::isNoopCast(Instruction::CastOps Opcode, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2079 | Type *SrcTy, |
| 2080 | Type *DestTy, |
| 2081 | Type *IntPtrTy) { |
Dan Gohman | 2c048ea | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2082 | switch (Opcode) { |
Craig Topper | 50bee42 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2083 | default: llvm_unreachable("Invalid CastOp"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2084 | case Instruction::Trunc: |
| 2085 | case Instruction::ZExt: |
| 2086 | case Instruction::SExt: |
| 2087 | case Instruction::FPTrunc: |
| 2088 | case Instruction::FPExt: |
| 2089 | case Instruction::UIToFP: |
| 2090 | case Instruction::SIToFP: |
| 2091 | case Instruction::FPToUI: |
| 2092 | case Instruction::FPToSI: |
| 2093 | return false; // These always modify bits |
| 2094 | case Instruction::BitCast: |
| 2095 | return true; // BitCast never modifies bits. |
| 2096 | case Instruction::PtrToInt: |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2097 | return IntPtrTy->getScalarSizeInBits() == |
Dan Gohman | 2c048ea | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2098 | DestTy->getScalarSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2099 | case Instruction::IntToPtr: |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2100 | return IntPtrTy->getScalarSizeInBits() == |
Dan Gohman | 2c048ea | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2101 | SrcTy->getScalarSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2102 | } |
| 2103 | } |
| 2104 | |
Dan Gohman | 2c048ea | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2105 | /// @brief Determine if a cast is a no-op. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2106 | bool CastInst::isNoopCast(Type *IntPtrTy) const { |
Dan Gohman | 2c048ea | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2107 | return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy); |
| 2108 | } |
| 2109 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2110 | /// This function determines if a pair of casts can be eliminated and what |
| 2111 | /// opcode should be used in the elimination. This assumes that there are two |
| 2112 | /// instructions like this: |
| 2113 | /// * %F = firstOpcode SrcTy %x to MidTy |
| 2114 | /// * %S = secondOpcode MidTy %F to DstTy |
| 2115 | /// The function returns a resultOpcode so these two casts can be replaced with: |
| 2116 | /// * %Replacement = resultOpcode %SrcTy %x to DstTy |
| 2117 | /// If no such cast is permited, the function returns 0. |
| 2118 | unsigned CastInst::isEliminableCastPair( |
| 2119 | Instruction::CastOps firstOp, Instruction::CastOps secondOp, |
Duncan Sands | 446cf94 | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2120 | Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy, |
| 2121 | Type *DstIntPtrTy) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2122 | // Define the 144 possibilities for these two cast instructions. The values |
| 2123 | // in this matrix determine what to do in a given situation and select the |
| 2124 | // case in the switch below. The rows correspond to firstOp, the columns |
| 2125 | // correspond to secondOp. In looking at the table below, keep in mind |
| 2126 | // the following cast properties: |
| 2127 | // |
| 2128 | // Size Compare Source Destination |
| 2129 | // Operator Src ? Size Type Sign Type Sign |
| 2130 | // -------- ------------ ------------------- --------------------- |
| 2131 | // TRUNC > Integer Any Integral Any |
| 2132 | // ZEXT < Integral Unsigned Integer Any |
| 2133 | // SEXT < Integral Signed Integer Any |
| 2134 | // FPTOUI n/a FloatPt n/a Integral Unsigned |
| 2135 | // FPTOSI n/a FloatPt n/a Integral Signed |
| 2136 | // UITOFP n/a Integral Unsigned FloatPt n/a |
| 2137 | // SITOFP n/a Integral Signed FloatPt n/a |
| 2138 | // FPTRUNC > FloatPt n/a FloatPt n/a |
| 2139 | // FPEXT < FloatPt n/a FloatPt n/a |
| 2140 | // PTRTOINT n/a Pointer n/a Integral Unsigned |
| 2141 | // INTTOPTR n/a Integral Unsigned Pointer n/a |
Dan Gohman | a5ced59 | 2010-04-07 23:22:42 +0000 | [diff] [blame] | 2142 | // BITCAST = FirstClass n/a FirstClass n/a |
Chris Lattner | 518f6fa | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2143 | // |
| 2144 | // NOTE: some transforms are safe, but we consider them to be non-profitable. |
Dan Gohman | a119de8 | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 2145 | // For example, we could merge "fptoui double to i32" + "zext i32 to i64", |
| 2146 | // into "fptoui double to i64", but this loses information about the range |
Chris Lattner | 518f6fa | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2147 | // of the produced value (we no longer know the top-part is all zeros). |
| 2148 | // Further this conversion is often much more expensive for typical hardware, |
| 2149 | // and causes issues when building libgcc. We disallow fptosi+sext for the |
| 2150 | // same reason. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2151 | const unsigned numCastOps = |
| 2152 | Instruction::CastOpsEnd - Instruction::CastOpsBegin; |
| 2153 | static const uint8_t CastResults[numCastOps][numCastOps] = { |
| 2154 | // T F F U S F F P I B -+ |
| 2155 | // R Z S P P I I T P 2 N T | |
| 2156 | // U E E 2 2 2 2 R E I T C +- secondOp |
| 2157 | // N X X U S F F N X N 2 V | |
| 2158 | // C T T I I P P C T T P T -+ |
| 2159 | { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+ |
| 2160 | { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt | |
| 2161 | { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt | |
Chris Lattner | 518f6fa | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2162 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI | |
| 2163 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2164 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp |
| 2165 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP | |
| 2166 | { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc | |
| 2167 | { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt | |
| 2168 | { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt | |
| 2169 | { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr | |
| 2170 | { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+ |
| 2171 | }; |
Chris Lattner | dfd3626 | 2010-07-12 01:19:22 +0000 | [diff] [blame] | 2172 | |
| 2173 | // If either of the casts are a bitcast from scalar to vector, disallow the |
Nadav Rotem | 89879ec | 2011-08-29 19:58:36 +0000 | [diff] [blame] | 2174 | // merging. However, bitcast of A->B->A are allowed. |
| 2175 | bool isFirstBitcast = (firstOp == Instruction::BitCast); |
| 2176 | bool isSecondBitcast = (secondOp == Instruction::BitCast); |
| 2177 | bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast); |
| 2178 | |
| 2179 | // Check if any of the bitcasts convert scalars<->vectors. |
| 2180 | if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) || |
| 2181 | (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy))) |
| 2182 | // Unless we are bitcasing to the original type, disallow optimizations. |
| 2183 | if (!chainedBitcast) return 0; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2184 | |
| 2185 | int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] |
| 2186 | [secondOp-Instruction::CastOpsBegin]; |
| 2187 | switch (ElimCase) { |
| 2188 | case 0: |
| 2189 | // categorically disallowed |
| 2190 | return 0; |
| 2191 | case 1: |
| 2192 | // allowed, use first cast's opcode |
| 2193 | return firstOp; |
| 2194 | case 2: |
| 2195 | // allowed, use second cast's opcode |
| 2196 | return secondOp; |
| 2197 | case 3: |
| 2198 | // no-op cast in second op implies firstOp as long as the DestTy |
Mon P Wang | e4a0a15 | 2010-01-23 04:35:57 +0000 | [diff] [blame] | 2199 | // is integer and we are not converting between a vector and a |
Chris Lattner | 33b1758 | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2200 | // non vector type. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2201 | if (!SrcTy->isVectorTy() && DstTy->isIntegerTy()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2202 | return firstOp; |
| 2203 | return 0; |
| 2204 | case 4: |
| 2205 | // no-op cast in second op implies firstOp as long as the DestTy |
Chris Lattner | 33b1758 | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2206 | // is floating point. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2207 | if (DstTy->isFloatingPointTy()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2208 | return firstOp; |
| 2209 | return 0; |
| 2210 | case 5: |
| 2211 | // no-op cast in first op implies secondOp as long as the SrcTy |
Chris Lattner | 33b1758 | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2212 | // is an integer. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2213 | if (SrcTy->isIntegerTy()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2214 | return secondOp; |
| 2215 | return 0; |
| 2216 | case 6: |
| 2217 | // no-op cast in first op implies secondOp as long as the SrcTy |
Chris Lattner | 33b1758 | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2218 | // is a floating point. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2219 | if (SrcTy->isFloatingPointTy()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2220 | return secondOp; |
| 2221 | return 0; |
| 2222 | case 7: { |
| 2223 | // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size |
Duncan Sands | 446cf94 | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2224 | if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy) |
Dan Gohman | 295643b | 2009-07-21 23:19:40 +0000 | [diff] [blame] | 2225 | return 0; |
Duncan Sands | 446cf94 | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2226 | unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits(); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2227 | unsigned MidSize = MidTy->getScalarSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2228 | if (MidSize >= PtrSize) |
| 2229 | return Instruction::BitCast; |
| 2230 | return 0; |
| 2231 | } |
| 2232 | case 8: { |
| 2233 | // ext, trunc -> bitcast, if the SrcTy and DstTy are same size |
| 2234 | // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) |
| 2235 | // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2236 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); |
| 2237 | unsigned DstSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2238 | if (SrcSize == DstSize) |
| 2239 | return Instruction::BitCast; |
| 2240 | else if (SrcSize < DstSize) |
| 2241 | return firstOp; |
| 2242 | return secondOp; |
| 2243 | } |
| 2244 | case 9: // zext, sext -> zext, because sext can't sign extend after zext |
| 2245 | return Instruction::ZExt; |
| 2246 | case 10: |
| 2247 | // fpext followed by ftrunc is allowed if the bit size returned to is |
| 2248 | // the same as the original, in which case its just a bitcast |
| 2249 | if (SrcTy == DstTy) |
| 2250 | return Instruction::BitCast; |
| 2251 | return 0; // If the types are not the same we can't eliminate it. |
| 2252 | case 11: |
| 2253 | // bitcast followed by ptrtoint is allowed as long as the bitcast |
| 2254 | // is a pointer to pointer cast. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2255 | if (SrcTy->isPointerTy() && MidTy->isPointerTy()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2256 | return secondOp; |
| 2257 | return 0; |
| 2258 | case 12: |
| 2259 | // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2260 | if (MidTy->isPointerTy() && DstTy->isPointerTy()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2261 | return firstOp; |
| 2262 | return 0; |
| 2263 | case 13: { |
| 2264 | // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize |
Duncan Sands | 446cf94 | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2265 | if (!MidIntPtrTy) |
Dan Gohman | 295643b | 2009-07-21 23:19:40 +0000 | [diff] [blame] | 2266 | return 0; |
Duncan Sands | 446cf94 | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2267 | unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits(); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2268 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); |
| 2269 | unsigned DstSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2270 | if (SrcSize <= PtrSize && SrcSize == DstSize) |
| 2271 | return Instruction::BitCast; |
| 2272 | return 0; |
| 2273 | } |
| 2274 | case 99: |
| 2275 | // cast combination can't happen (error in input). This is for all cases |
| 2276 | // where the MidTy is not the same for the two cast instructions. |
Craig Topper | 50bee42 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2277 | llvm_unreachable("Invalid Cast Combination"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2278 | default: |
Craig Topper | 50bee42 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2279 | llvm_unreachable("Error in CastResults table!!!"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2280 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2283 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2284 | const Twine &Name, Instruction *InsertBefore) { |
Duncan Sands | bb1695e | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2285 | assert(castIsValid(op, S, Ty) && "Invalid cast!"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2286 | // Construct and return the appropriate CastInst subclass |
| 2287 | switch (op) { |
| 2288 | case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); |
| 2289 | case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); |
| 2290 | case SExt: return new SExtInst (S, Ty, Name, InsertBefore); |
| 2291 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); |
| 2292 | case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); |
| 2293 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); |
| 2294 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); |
| 2295 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); |
| 2296 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); |
| 2297 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); |
| 2298 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); |
| 2299 | case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); |
Craig Topper | 50bee42 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2300 | default: llvm_unreachable("Invalid opcode provided"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2301 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2304 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2305 | const Twine &Name, BasicBlock *InsertAtEnd) { |
Duncan Sands | bb1695e | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2306 | assert(castIsValid(op, S, Ty) && "Invalid cast!"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2307 | // Construct and return the appropriate CastInst subclass |
| 2308 | switch (op) { |
| 2309 | case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd); |
| 2310 | case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd); |
| 2311 | case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd); |
| 2312 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd); |
| 2313 | case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd); |
| 2314 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd); |
| 2315 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd); |
| 2316 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd); |
| 2317 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd); |
| 2318 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); |
| 2319 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); |
| 2320 | case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); |
Craig Topper | 50bee42 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2321 | default: llvm_unreachable("Invalid opcode provided"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2322 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2325 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2326 | const Twine &Name, |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2327 | Instruction *InsertBefore) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2328 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2329 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2330 | return Create(Instruction::ZExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2331 | } |
| 2332 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2333 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2334 | const Twine &Name, |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2335 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2336 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2337 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2338 | return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2339 | } |
| 2340 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2341 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2342 | const Twine &Name, |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2343 | Instruction *InsertBefore) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2344 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2345 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2346 | return Create(Instruction::SExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2349 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2350 | const Twine &Name, |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2351 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2352 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2353 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2354 | return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2355 | } |
| 2356 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2357 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2358 | const Twine &Name, |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2359 | Instruction *InsertBefore) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2360 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2361 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2362 | return Create(Instruction::Trunc, S, Ty, Name, InsertBefore); |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2365 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2366 | const Twine &Name, |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2367 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2368 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2369 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2370 | return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 848414e | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2373 | CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2374 | const Twine &Name, |
Reid Spencer | 330d86d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2375 | BasicBlock *InsertAtEnd) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2376 | assert(S->getType()->isPointerTy() && "Invalid cast"); |
| 2377 | assert((Ty->isIntegerTy() || Ty->isPointerTy()) && |
Reid Spencer | 330d86d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2378 | "Invalid cast"); |
| 2379 | |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2380 | if (Ty->isIntegerTy()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2381 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); |
| 2382 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 330d86d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
| 2385 | /// @brief Create a BitCast or a PtrToInt cast instruction |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2386 | CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2387 | const Twine &Name, |
Reid Spencer | 330d86d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2388 | Instruction *InsertBefore) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2389 | assert(S->getType()->isPointerTy() && "Invalid cast"); |
| 2390 | assert((Ty->isIntegerTy() || Ty->isPointerTy()) && |
Reid Spencer | 330d86d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2391 | "Invalid cast"); |
| 2392 | |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2393 | if (Ty->isIntegerTy()) |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2394 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); |
| 2395 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
Reid Spencer | 330d86d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2396 | } |
| 2397 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2398 | CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2399 | bool isSigned, const Twine &Name, |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2400 | Instruction *InsertBefore) { |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2401 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && |
Chris Lattner | 859c372 | 2010-01-10 20:21:42 +0000 | [diff] [blame] | 2402 | "Invalid integer cast"); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2403 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2404 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2405 | Instruction::CastOps opcode = |
| 2406 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2407 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2408 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2409 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2412 | CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2413 | bool isSigned, const Twine &Name, |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2414 | BasicBlock *InsertAtEnd) { |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2415 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && |
Dan Gohman | f57478f | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2416 | "Invalid cast"); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2417 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2418 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2419 | Instruction::CastOps opcode = |
| 2420 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2421 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2422 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2423 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2424 | } |
| 2425 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2426 | CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2427 | const Twine &Name, |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2428 | Instruction *InsertBefore) { |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2429 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2430 | "Invalid cast"); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2431 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2432 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2433 | Instruction::CastOps opcode = |
| 2434 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2435 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2436 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2437 | } |
| 2438 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2439 | CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2440 | const Twine &Name, |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2441 | BasicBlock *InsertAtEnd) { |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2442 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2443 | "Invalid cast"); |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2444 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2445 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2446 | Instruction::CastOps opcode = |
| 2447 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2448 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2449 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 6d81a7d | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2452 | // Check whether it is valid to call getCastOpcode for these types. |
| 2453 | // This routine must be kept in sync with getCastOpcode. |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2454 | bool CastInst::isCastable(Type *SrcTy, Type *DestTy) { |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2455 | if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) |
| 2456 | return false; |
| 2457 | |
| 2458 | if (SrcTy == DestTy) |
| 2459 | return true; |
| 2460 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2461 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) |
| 2462 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) |
Duncan Sands | 117feba | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2463 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 2464 | // An element by element cast. Valid if casting the elements is valid. |
| 2465 | SrcTy = SrcVecTy->getElementType(); |
| 2466 | DestTy = DestVecTy->getElementType(); |
| 2467 | } |
| 2468 | |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2469 | // Get the bit sizes, we'll need these |
Duncan Sands | bb1695e | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2470 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2471 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2472 | |
| 2473 | // Run through the possibilities ... |
Duncan Sands | 7016ec1 | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2474 | if (DestTy->isIntegerTy()) { // Casting to integral |
| 2475 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2476 | return true; |
Duncan Sands | 7016ec1 | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2477 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2478 | return true; |
Duncan Sands | 7016ec1 | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2479 | } else if (SrcTy->isVectorTy()) { // Casting from vector |
| 2480 | return DestBits == SrcBits; |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2481 | } else { // Casting from something else |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2482 | return SrcTy->isPointerTy(); |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2483 | } |
Duncan Sands | 7016ec1 | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2484 | } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt |
| 2485 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2486 | return true; |
Duncan Sands | 7016ec1 | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2487 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2488 | return true; |
Duncan Sands | 7016ec1 | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2489 | } else if (SrcTy->isVectorTy()) { // Casting from vector |
| 2490 | return DestBits == SrcBits; |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2491 | } else { // Casting from something else |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2492 | return false; |
| 2493 | } |
Duncan Sands | 7016ec1 | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2494 | } else if (DestTy->isVectorTy()) { // Casting to vector |
| 2495 | return DestBits == SrcBits; |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2496 | } else if (DestTy->isPointerTy()) { // Casting to pointer |
Duncan Sands | 7016ec1 | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2497 | if (SrcTy->isPointerTy()) { // Casting from pointer |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2498 | return true; |
Duncan Sands | 7016ec1 | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2499 | } else if (SrcTy->isIntegerTy()) { // Casting from integral |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2500 | return true; |
Duncan Sands | 7016ec1 | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2501 | } else { // Casting from something else |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2502 | return false; |
| 2503 | } |
Duncan Sands | 6079465 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 2504 | } else if (DestTy->isX86_MMXTy()) { |
Duncan Sands | 7016ec1 | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2505 | if (SrcTy->isVectorTy()) { |
| 2506 | return DestBits == SrcBits; // 64-bit vector to MMX |
Duncan Sands | 6079465 | 2011-04-01 03:34:54 +0000 | [diff] [blame] | 2507 | } else { |
| 2508 | return false; |
| 2509 | } |
Duncan Sands | 7016ec1 | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2510 | } else { // Casting to something else |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2511 | return false; |
| 2512 | } |
| 2513 | } |
| 2514 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2515 | // Provide a way to get a "cast" where the cast opcode is inferred from the |
| 2516 | // types and size of the operand. This, basically, is a parallel of the |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2517 | // logic in the castIsValid function below. This axiom should hold: |
| 2518 | // castIsValid( getCastOpcode(Val, Ty), Val, Ty) |
| 2519 | // should not assert in castIsValid. In other words, this produces a "correct" |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2520 | // casting opcode for the arguments passed to it. |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2521 | // This routine must be kept in sync with isCastable. |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2522 | Instruction::CastOps |
Reid Spencer | 5666712 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2523 | CastInst::getCastOpcode( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2524 | const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) { |
| 2525 | Type *SrcTy = Src->getType(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2526 | |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2527 | assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() && |
| 2528 | "Only first class types are castable!"); |
| 2529 | |
Duncan Sands | 117feba | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2530 | if (SrcTy == DestTy) |
| 2531 | return BitCast; |
| 2532 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2533 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) |
| 2534 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) |
Duncan Sands | 117feba | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2535 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 2536 | // An element by element cast. Find the appropriate opcode based on the |
| 2537 | // element types. |
| 2538 | SrcTy = SrcVecTy->getElementType(); |
| 2539 | DestTy = DestVecTy->getElementType(); |
| 2540 | } |
| 2541 | |
| 2542 | // Get the bit sizes, we'll need these |
Duncan Sands | bb1695e | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2543 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2544 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
Duncan Sands | 117feba | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2545 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2546 | // Run through the possibilities ... |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2547 | if (DestTy->isIntegerTy()) { // Casting to integral |
| 2548 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2549 | if (DestBits < SrcBits) |
| 2550 | return Trunc; // int -> smaller int |
| 2551 | else if (DestBits > SrcBits) { // its an extension |
Reid Spencer | 5666712 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2552 | if (SrcIsSigned) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2553 | return SExt; // signed -> SEXT |
| 2554 | else |
| 2555 | return ZExt; // unsigned -> ZEXT |
| 2556 | } else { |
| 2557 | return BitCast; // Same size, No-op cast |
| 2558 | } |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2559 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Reid Spencer | 5666712 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2560 | if (DestIsSigned) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2561 | return FPToSI; // FP -> sint |
| 2562 | else |
| 2563 | return FPToUI; // FP -> uint |
Duncan Sands | 7016ec1 | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2564 | } else if (SrcTy->isVectorTy()) { |
| 2565 | assert(DestBits == SrcBits && |
| 2566 | "Casting vector to integer of different width"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2567 | return BitCast; // Same size, no-op cast |
| 2568 | } else { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2569 | assert(SrcTy->isPointerTy() && |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2570 | "Casting from a value that is not first-class type"); |
| 2571 | return PtrToInt; // ptr -> int |
| 2572 | } |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2573 | } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt |
| 2574 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Reid Spencer | 5666712 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2575 | if (SrcIsSigned) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2576 | return SIToFP; // sint -> FP |
| 2577 | else |
| 2578 | return UIToFP; // uint -> FP |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2579 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2580 | if (DestBits < SrcBits) { |
| 2581 | return FPTrunc; // FP -> smaller FP |
| 2582 | } else if (DestBits > SrcBits) { |
| 2583 | return FPExt; // FP -> larger FP |
| 2584 | } else { |
| 2585 | return BitCast; // same size, no-op cast |
| 2586 | } |
Duncan Sands | 7016ec1 | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2587 | } else if (SrcTy->isVectorTy()) { |
| 2588 | assert(DestBits == SrcBits && |
Dan Gohman | 86296cc | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2589 | "Casting vector to floating point of different width"); |
Devang Patel | 8c3b47f | 2008-11-05 01:37:40 +0000 | [diff] [blame] | 2590 | return BitCast; // same size, no-op cast |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2591 | } |
Ahmed Charles | b0934ab | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 2592 | llvm_unreachable("Casting pointer or non-first class to float"); |
Duncan Sands | 7016ec1 | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2593 | } else if (DestTy->isVectorTy()) { |
| 2594 | assert(DestBits == SrcBits && |
| 2595 | "Illegal cast to vector (wrong type or size)"); |
| 2596 | return BitCast; |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2597 | } else if (DestTy->isPointerTy()) { |
| 2598 | if (SrcTy->isPointerTy()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2599 | return BitCast; // ptr -> ptr |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2600 | } else if (SrcTy->isIntegerTy()) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2601 | return IntToPtr; // int -> ptr |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2602 | } |
Ahmed Charles | b0934ab | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 2603 | llvm_unreachable("Casting pointer to other than pointer or int"); |
Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 2604 | } else if (DestTy->isX86_MMXTy()) { |
Duncan Sands | 7016ec1 | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2605 | if (SrcTy->isVectorTy()) { |
| 2606 | assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX"); |
Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 2607 | return BitCast; // 64-bit vector to MMX |
Dale Johannesen | 0488fb6 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 2608 | } |
Ahmed Charles | b0934ab | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 2609 | llvm_unreachable("Illegal cast to X86_MMX"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2610 | } |
Ahmed Charles | b0934ab | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 2611 | llvm_unreachable("Casting to type that is not first-class"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2612 | } |
| 2613 | |
| 2614 | //===----------------------------------------------------------------------===// |
| 2615 | // CastInst SubClass Constructors |
| 2616 | //===----------------------------------------------------------------------===// |
| 2617 | |
| 2618 | /// Check that the construction parameters for a CastInst are correct. This |
| 2619 | /// could be broken out into the separate constructors but it is useful to have |
| 2620 | /// it in one place and to eliminate the redundant code for getting the sizes |
| 2621 | /// of the types involved. |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2622 | bool |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2623 | CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) { |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2624 | |
| 2625 | // Check for type sanity on the arguments |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2626 | Type *SrcTy = S->getType(); |
Chris Lattner | 0b68a00 | 2010-01-26 21:51:43 +0000 | [diff] [blame] | 2627 | if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() || |
| 2628 | SrcTy->isAggregateType() || DstTy->isAggregateType()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2629 | return false; |
| 2630 | |
| 2631 | // Get the size of the types in bits, we'll need this later |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2632 | unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 2633 | unsigned DstBitSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2634 | |
Duncan Sands | bb1695e | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2635 | // If these are vector types, get the lengths of the vectors (using zero for |
| 2636 | // scalar types means that checking that vector lengths match also checks that |
| 2637 | // scalars are not being converted to vectors or vectors to scalars). |
| 2638 | unsigned SrcLength = SrcTy->isVectorTy() ? |
| 2639 | cast<VectorType>(SrcTy)->getNumElements() : 0; |
| 2640 | unsigned DstLength = DstTy->isVectorTy() ? |
| 2641 | cast<VectorType>(DstTy)->getNumElements() : 0; |
| 2642 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2643 | // Switch on the opcode provided |
| 2644 | switch (op) { |
| 2645 | default: return false; // This is an input error |
| 2646 | case Instruction::Trunc: |
Duncan Sands | bb1695e | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2647 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 2648 | SrcLength == DstLength && SrcBitSize > DstBitSize; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2649 | case Instruction::ZExt: |
Duncan Sands | bb1695e | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2650 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 2651 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2652 | case Instruction::SExt: |
Duncan Sands | bb1695e | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2653 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 2654 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2655 | case Instruction::FPTrunc: |
Duncan Sands | bb1695e | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2656 | return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 2657 | SrcLength == DstLength && SrcBitSize > DstBitSize; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2658 | case Instruction::FPExt: |
Duncan Sands | bb1695e | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2659 | return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 2660 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2661 | case Instruction::UIToFP: |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2662 | case Instruction::SIToFP: |
Duncan Sands | bb1695e | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2663 | return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 2664 | SrcLength == DstLength; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2665 | case Instruction::FPToUI: |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2666 | case Instruction::FPToSI: |
Duncan Sands | bb1695e | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2667 | return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 2668 | SrcLength == DstLength; |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2669 | case Instruction::PtrToInt: |
Chris Lattner | af7b4fb | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 2670 | if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy)) |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 2671 | return false; |
Chris Lattner | af7b4fb | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 2672 | if (VectorType *VT = dyn_cast<VectorType>(SrcTy)) |
| 2673 | if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements()) |
| 2674 | return false; |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 2675 | return SrcTy->getScalarType()->isPointerTy() && |
| 2676 | DstTy->getScalarType()->isIntegerTy(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2677 | case Instruction::IntToPtr: |
Chris Lattner | af7b4fb | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 2678 | if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy)) |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 2679 | return false; |
Chris Lattner | af7b4fb | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 2680 | if (VectorType *VT = dyn_cast<VectorType>(SrcTy)) |
| 2681 | if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements()) |
| 2682 | return false; |
Nadav Rotem | 1608769 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 2683 | return SrcTy->getScalarType()->isIntegerTy() && |
| 2684 | DstTy->getScalarType()->isPointerTy(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2685 | case Instruction::BitCast: |
| 2686 | // BitCast implies a no-op cast of type only. No bits change. |
| 2687 | // However, you can't cast pointers to anything but pointers. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2688 | if (SrcTy->isPointerTy() != DstTy->isPointerTy()) |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2689 | return false; |
| 2690 | |
Duncan Sands | a9d0c9d | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2691 | // Now we know we're not dealing with a pointer/non-pointer mismatch. In all |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2692 | // these cases, the cast is okay if the source and destination bit widths |
| 2693 | // are identical. |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2694 | return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits(); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2695 | } |
| 2696 | } |
| 2697 | |
| 2698 | TruncInst::TruncInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2699 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2700 | ) : CastInst(Ty, Trunc, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2701 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2702 | } |
| 2703 | |
| 2704 | TruncInst::TruncInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2705 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2706 | ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2707 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2708 | } |
| 2709 | |
| 2710 | ZExtInst::ZExtInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2711 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2712 | ) : CastInst(Ty, ZExt, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2713 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2714 | } |
| 2715 | |
| 2716 | ZExtInst::ZExtInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2717 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2718 | ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2719 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2720 | } |
| 2721 | SExtInst::SExtInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2722 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2723 | ) : CastInst(Ty, SExt, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2724 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2725 | } |
| 2726 | |
Jeff Cohen | 97af751 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 2727 | SExtInst::SExtInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2728 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2729 | ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2730 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2731 | } |
| 2732 | |
| 2733 | FPTruncInst::FPTruncInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2734 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2735 | ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2736 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
| 2739 | FPTruncInst::FPTruncInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2740 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2741 | ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2742 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2743 | } |
| 2744 | |
| 2745 | FPExtInst::FPExtInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2746 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2747 | ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2748 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2749 | } |
| 2750 | |
| 2751 | FPExtInst::FPExtInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2752 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2753 | ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2754 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2755 | } |
| 2756 | |
| 2757 | UIToFPInst::UIToFPInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2758 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2759 | ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2760 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2761 | } |
| 2762 | |
| 2763 | UIToFPInst::UIToFPInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2764 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2765 | ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2766 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2767 | } |
| 2768 | |
| 2769 | SIToFPInst::SIToFPInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2770 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2771 | ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2772 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2773 | } |
| 2774 | |
| 2775 | SIToFPInst::SIToFPInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2776 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2777 | ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2778 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
| 2781 | FPToUIInst::FPToUIInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2782 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2783 | ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2784 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2785 | } |
| 2786 | |
| 2787 | FPToUIInst::FPToUIInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2788 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2789 | ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2790 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
| 2793 | FPToSIInst::FPToSIInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2794 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2795 | ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2796 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2797 | } |
| 2798 | |
| 2799 | FPToSIInst::FPToSIInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2800 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2801 | ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2802 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2803 | } |
| 2804 | |
| 2805 | PtrToIntInst::PtrToIntInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2806 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2807 | ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2808 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
| 2811 | PtrToIntInst::PtrToIntInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2812 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2813 | ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2814 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2815 | } |
| 2816 | |
| 2817 | IntToPtrInst::IntToPtrInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2818 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2819 | ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2820 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | IntToPtrInst::IntToPtrInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2824 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2825 | ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2826 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
| 2829 | BitCastInst::BitCastInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2830 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2831 | ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2832 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
| 2835 | BitCastInst::BitCastInst( |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2836 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2837 | ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { |
Reid Spencer | 0a11af1 | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2838 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2839 | } |
Chris Lattner | 2f46386 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 2840 | |
| 2841 | //===----------------------------------------------------------------------===// |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2842 | // CmpInst Classes |
| 2843 | //===----------------------------------------------------------------------===// |
| 2844 | |
Craig Topper | a96a182 | 2012-09-23 02:12:10 +0000 | [diff] [blame] | 2845 | void CmpInst::anchor() {} |
Chris Lattner | 36fe198 | 2010-01-22 06:25:37 +0000 | [diff] [blame] | 2846 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2847 | CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2848 | Value *LHS, Value *RHS, const Twine &Name, |
Nate Begeman | ac80ade | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 2849 | Instruction *InsertBefore) |
Nate Begeman | c83ad0d | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 2850 | : Instruction(ty, op, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2851 | OperandTraits<CmpInst>::op_begin(this), |
| 2852 | OperandTraits<CmpInst>::operands(this), |
| 2853 | InsertBefore) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2854 | Op<0>() = LHS; |
| 2855 | Op<1>() = RHS; |
Chris Lattner | cafe9bb | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 2856 | setPredicate((Predicate)predicate); |
Reid Spencer | 97c0e21 | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 2857 | setName(Name); |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2858 | } |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2859 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2860 | CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2861 | Value *LHS, Value *RHS, const Twine &Name, |
Nate Begeman | ac80ade | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 2862 | BasicBlock *InsertAtEnd) |
Nate Begeman | c83ad0d | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 2863 | : Instruction(ty, op, |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2864 | OperandTraits<CmpInst>::op_begin(this), |
| 2865 | OperandTraits<CmpInst>::operands(this), |
| 2866 | InsertAtEnd) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2867 | Op<0>() = LHS; |
| 2868 | Op<1>() = RHS; |
Chris Lattner | cafe9bb | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 2869 | setPredicate((Predicate)predicate); |
Reid Spencer | 97c0e21 | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 2870 | setName(Name); |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
| 2873 | CmpInst * |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2874 | CmpInst::Create(OtherOps Op, unsigned short predicate, |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2875 | Value *S1, Value *S2, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2876 | const Twine &Name, Instruction *InsertBefore) { |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2877 | if (Op == Instruction::ICmp) { |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2878 | if (InsertBefore) |
| 2879 | return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate), |
| 2880 | S1, S2, Name); |
| 2881 | else |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2882 | return new ICmpInst(CmpInst::Predicate(predicate), |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2883 | S1, S2, Name); |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2884 | } |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2885 | |
| 2886 | if (InsertBefore) |
| 2887 | return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate), |
| 2888 | S1, S2, Name); |
| 2889 | else |
Dan Gohman | 1c8a23c | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2890 | return new FCmpInst(CmpInst::Predicate(predicate), |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2891 | S1, S2, Name); |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
| 2894 | CmpInst * |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2895 | CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, |
Daniel Dunbar | 6e0d1cb | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2896 | const Twine &Name, BasicBlock *InsertAtEnd) { |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2897 | if (Op == Instruction::ICmp) { |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2898 | return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), |
| 2899 | S1, S2, Name); |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2900 | } |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2901 | return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), |
| 2902 | S1, S2, Name); |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2903 | } |
| 2904 | |
| 2905 | void CmpInst::swapOperands() { |
| 2906 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 2907 | IC->swapOperands(); |
| 2908 | else |
| 2909 | cast<FCmpInst>(this)->swapOperands(); |
| 2910 | } |
| 2911 | |
Duncan Sands | c449a22 | 2011-01-04 12:52:29 +0000 | [diff] [blame] | 2912 | bool CmpInst::isCommutative() const { |
| 2913 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2914 | return IC->isCommutative(); |
| 2915 | return cast<FCmpInst>(this)->isCommutative(); |
| 2916 | } |
| 2917 | |
Duncan Sands | c449a22 | 2011-01-04 12:52:29 +0000 | [diff] [blame] | 2918 | bool CmpInst::isEquality() const { |
| 2919 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2920 | return IC->isEquality(); |
| 2921 | return cast<FCmpInst>(this)->isEquality(); |
| 2922 | } |
| 2923 | |
| 2924 | |
Dan Gohman | 7e2dd66 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 2925 | CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) { |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2926 | switch (pred) { |
Craig Topper | 50bee42 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2927 | default: llvm_unreachable("Unknown cmp predicate!"); |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2928 | case ICMP_EQ: return ICMP_NE; |
| 2929 | case ICMP_NE: return ICMP_EQ; |
| 2930 | case ICMP_UGT: return ICMP_ULE; |
| 2931 | case ICMP_ULT: return ICMP_UGE; |
| 2932 | case ICMP_UGE: return ICMP_ULT; |
| 2933 | case ICMP_ULE: return ICMP_UGT; |
| 2934 | case ICMP_SGT: return ICMP_SLE; |
| 2935 | case ICMP_SLT: return ICMP_SGE; |
| 2936 | case ICMP_SGE: return ICMP_SLT; |
| 2937 | case ICMP_SLE: return ICMP_SGT; |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2938 | |
Dan Gohman | 7e2dd66 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 2939 | case FCMP_OEQ: return FCMP_UNE; |
| 2940 | case FCMP_ONE: return FCMP_UEQ; |
| 2941 | case FCMP_OGT: return FCMP_ULE; |
| 2942 | case FCMP_OLT: return FCMP_UGE; |
| 2943 | case FCMP_OGE: return FCMP_ULT; |
| 2944 | case FCMP_OLE: return FCMP_UGT; |
| 2945 | case FCMP_UEQ: return FCMP_ONE; |
| 2946 | case FCMP_UNE: return FCMP_OEQ; |
| 2947 | case FCMP_UGT: return FCMP_OLE; |
| 2948 | case FCMP_ULT: return FCMP_OGE; |
| 2949 | case FCMP_UGE: return FCMP_OLT; |
| 2950 | case FCMP_ULE: return FCMP_OGT; |
| 2951 | case FCMP_ORD: return FCMP_UNO; |
| 2952 | case FCMP_UNO: return FCMP_ORD; |
| 2953 | case FCMP_TRUE: return FCMP_FALSE; |
| 2954 | case FCMP_FALSE: return FCMP_TRUE; |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2955 | } |
| 2956 | } |
| 2957 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2958 | ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { |
| 2959 | switch (pred) { |
Craig Topper | 50bee42 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2960 | default: llvm_unreachable("Unknown icmp predicate!"); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2961 | case ICMP_EQ: case ICMP_NE: |
| 2962 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: |
| 2963 | return pred; |
| 2964 | case ICMP_UGT: return ICMP_SGT; |
| 2965 | case ICMP_ULT: return ICMP_SLT; |
| 2966 | case ICMP_UGE: return ICMP_SGE; |
| 2967 | case ICMP_ULE: return ICMP_SLE; |
| 2968 | } |
| 2969 | } |
| 2970 | |
Nick Lewycky | 4189a53 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 2971 | ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) { |
| 2972 | switch (pred) { |
Craig Topper | 50bee42 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2973 | default: llvm_unreachable("Unknown icmp predicate!"); |
Nick Lewycky | 4189a53 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 2974 | case ICMP_EQ: case ICMP_NE: |
| 2975 | case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE: |
| 2976 | return pred; |
| 2977 | case ICMP_SGT: return ICMP_UGT; |
| 2978 | case ICMP_SLT: return ICMP_ULT; |
| 2979 | case ICMP_SGE: return ICMP_UGE; |
| 2980 | case ICMP_SLE: return ICMP_ULE; |
| 2981 | } |
| 2982 | } |
| 2983 | |
Reid Spencer | 3da4384 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2984 | /// Initialize a set of values that all satisfy the condition with C. |
| 2985 | /// |
| 2986 | ConstantRange |
| 2987 | ICmpInst::makeConstantRange(Predicate pred, const APInt &C) { |
| 2988 | APInt Lower(C); |
| 2989 | APInt Upper(C); |
| 2990 | uint32_t BitWidth = C.getBitWidth(); |
| 2991 | switch (pred) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2992 | default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!"); |
Reid Spencer | 3da4384 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2993 | case ICmpInst::ICMP_EQ: Upper++; break; |
| 2994 | case ICmpInst::ICMP_NE: Lower++; break; |
Dan Gohman | f21107c | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 2995 | case ICmpInst::ICMP_ULT: |
| 2996 | Lower = APInt::getMinValue(BitWidth); |
| 2997 | // Check for an empty-set condition. |
| 2998 | if (Lower == Upper) |
| 2999 | return ConstantRange(BitWidth, /*isFullSet=*/false); |
| 3000 | break; |
| 3001 | case ICmpInst::ICMP_SLT: |
| 3002 | Lower = APInt::getSignedMinValue(BitWidth); |
| 3003 | // Check for an empty-set condition. |
| 3004 | if (Lower == Upper) |
| 3005 | return ConstantRange(BitWidth, /*isFullSet=*/false); |
| 3006 | break; |
Reid Spencer | 3da4384 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3007 | case ICmpInst::ICMP_UGT: |
| 3008 | Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max) |
Dan Gohman | f21107c | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 3009 | // Check for an empty-set condition. |
| 3010 | if (Lower == Upper) |
| 3011 | return ConstantRange(BitWidth, /*isFullSet=*/false); |
Reid Spencer | 3da4384 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3012 | break; |
| 3013 | case ICmpInst::ICMP_SGT: |
| 3014 | Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max) |
Dan Gohman | f21107c | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 3015 | // Check for an empty-set condition. |
| 3016 | if (Lower == Upper) |
| 3017 | return ConstantRange(BitWidth, /*isFullSet=*/false); |
Reid Spencer | 3da4384 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3018 | break; |
| 3019 | case ICmpInst::ICMP_ULE: |
| 3020 | Lower = APInt::getMinValue(BitWidth); Upper++; |
Dan Gohman | f21107c | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 3021 | // Check for a full-set condition. |
| 3022 | if (Lower == Upper) |
| 3023 | return ConstantRange(BitWidth, /*isFullSet=*/true); |
Reid Spencer | 3da4384 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3024 | break; |
| 3025 | case ICmpInst::ICMP_SLE: |
| 3026 | Lower = APInt::getSignedMinValue(BitWidth); Upper++; |
Dan Gohman | f21107c | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 3027 | // Check for a full-set condition. |
| 3028 | if (Lower == Upper) |
| 3029 | return ConstantRange(BitWidth, /*isFullSet=*/true); |
Reid Spencer | 3da4384 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3030 | break; |
| 3031 | case ICmpInst::ICMP_UGE: |
| 3032 | Upper = APInt::getMinValue(BitWidth); // Min = Next(Max) |
Dan Gohman | f21107c | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 3033 | // Check for a full-set condition. |
| 3034 | if (Lower == Upper) |
| 3035 | return ConstantRange(BitWidth, /*isFullSet=*/true); |
Reid Spencer | 3da4384 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3036 | break; |
| 3037 | case ICmpInst::ICMP_SGE: |
| 3038 | Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max) |
Dan Gohman | f21107c | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 3039 | // Check for a full-set condition. |
| 3040 | if (Lower == Upper) |
| 3041 | return ConstantRange(BitWidth, /*isFullSet=*/true); |
Reid Spencer | 3da4384 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3042 | break; |
| 3043 | } |
| 3044 | return ConstantRange(Lower, Upper); |
| 3045 | } |
| 3046 | |
Dan Gohman | 7e2dd66 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3047 | CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) { |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3048 | switch (pred) { |
Craig Topper | 50bee42 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3049 | default: llvm_unreachable("Unknown cmp predicate!"); |
Dan Gohman | 7e2dd66 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3050 | case ICMP_EQ: case ICMP_NE: |
| 3051 | return pred; |
| 3052 | case ICMP_SGT: return ICMP_SLT; |
| 3053 | case ICMP_SLT: return ICMP_SGT; |
| 3054 | case ICMP_SGE: return ICMP_SLE; |
| 3055 | case ICMP_SLE: return ICMP_SGE; |
| 3056 | case ICMP_UGT: return ICMP_ULT; |
| 3057 | case ICMP_ULT: return ICMP_UGT; |
| 3058 | case ICMP_UGE: return ICMP_ULE; |
| 3059 | case ICMP_ULE: return ICMP_UGE; |
| 3060 | |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3061 | case FCMP_FALSE: case FCMP_TRUE: |
| 3062 | case FCMP_OEQ: case FCMP_ONE: |
| 3063 | case FCMP_UEQ: case FCMP_UNE: |
| 3064 | case FCMP_ORD: case FCMP_UNO: |
| 3065 | return pred; |
| 3066 | case FCMP_OGT: return FCMP_OLT; |
| 3067 | case FCMP_OLT: return FCMP_OGT; |
| 3068 | case FCMP_OGE: return FCMP_OLE; |
| 3069 | case FCMP_OLE: return FCMP_OGE; |
| 3070 | case FCMP_UGT: return FCMP_ULT; |
| 3071 | case FCMP_ULT: return FCMP_UGT; |
| 3072 | case FCMP_UGE: return FCMP_ULE; |
| 3073 | case FCMP_ULE: return FCMP_UGE; |
| 3074 | } |
| 3075 | } |
| 3076 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3077 | bool CmpInst::isUnsigned(unsigned short predicate) { |
| 3078 | switch (predicate) { |
| 3079 | default: return false; |
| 3080 | case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: |
| 3081 | case ICmpInst::ICMP_UGE: return true; |
| 3082 | } |
| 3083 | } |
| 3084 | |
Nick Lewycky | 44df023 | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 3085 | bool CmpInst::isSigned(unsigned short predicate) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3086 | switch (predicate) { |
| 3087 | default: return false; |
| 3088 | case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: |
| 3089 | case ICmpInst::ICMP_SGE: return true; |
| 3090 | } |
| 3091 | } |
| 3092 | |
| 3093 | bool CmpInst::isOrdered(unsigned short predicate) { |
| 3094 | switch (predicate) { |
| 3095 | default: return false; |
| 3096 | case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: |
| 3097 | case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: |
| 3098 | case FCmpInst::FCMP_ORD: return true; |
| 3099 | } |
| 3100 | } |
| 3101 | |
| 3102 | bool CmpInst::isUnordered(unsigned short predicate) { |
| 3103 | switch (predicate) { |
| 3104 | default: return false; |
| 3105 | case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: |
| 3106 | case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: |
| 3107 | case FCmpInst::FCMP_UNO: return true; |
| 3108 | } |
| 3109 | } |
| 3110 | |
Nick Lewycky | 44df023 | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 3111 | bool CmpInst::isTrueWhenEqual(unsigned short predicate) { |
| 3112 | switch(predicate) { |
| 3113 | default: return false; |
| 3114 | case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE: |
| 3115 | case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true; |
| 3116 | } |
| 3117 | } |
| 3118 | |
| 3119 | bool CmpInst::isFalseWhenEqual(unsigned short predicate) { |
| 3120 | switch(predicate) { |
| 3121 | case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT: |
| 3122 | case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true; |
| 3123 | default: return false; |
| 3124 | } |
| 3125 | } |
| 3126 | |
| 3127 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3128 | //===----------------------------------------------------------------------===// |
| 3129 | // SwitchInst Implementation |
| 3130 | //===----------------------------------------------------------------------===// |
| 3131 | |
Chris Lattner | aa6e350 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3132 | void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) { |
| 3133 | assert(Value && Default && NumReserved); |
| 3134 | ReservedSpace = NumReserved; |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3135 | NumOperands = 2; |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3136 | OperandList = allocHungoffUses(ReservedSpace); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3137 | |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3138 | OperandList[0] = Value; |
| 3139 | OperandList[1] = Default; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3140 | } |
| 3141 | |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3142 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 3143 | /// switch on and a default destination. The number of additional cases can |
| 3144 | /// be specified here to make memory allocation more efficient. This |
| 3145 | /// constructor can also autoinsert before another instruction. |
| 3146 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 3147 | Instruction *InsertBefore) |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3148 | : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch, |
| 3149 | 0, 0, InsertBefore) { |
Chris Lattner | aa6e350 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3150 | init(Value, Default, 2+NumCases*2); |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 3154 | /// switch on and a default destination. The number of additional cases can |
| 3155 | /// be specified here to make memory allocation more efficient. This |
| 3156 | /// constructor also autoinserts at the end of the specified BasicBlock. |
| 3157 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 3158 | BasicBlock *InsertAtEnd) |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3159 | : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch, |
| 3160 | 0, 0, InsertAtEnd) { |
Chris Lattner | aa6e350 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3161 | init(Value, Default, 2+NumCases*2); |
Chris Lattner | 910c80a | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3162 | } |
| 3163 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3164 | SwitchInst::SwitchInst(const SwitchInst &SI) |
Chris Lattner | aa6e350 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3165 | : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) { |
| 3166 | init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands()); |
| 3167 | NumOperands = SI.getNumOperands(); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3168 | Use *OL = OperandList, *InOL = SI.OperandList; |
Chris Lattner | aa6e350 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3169 | for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3170 | OL[i] = InOL[i]; |
| 3171 | OL[i+1] = InOL[i+1]; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3172 | } |
Stepan Dyatkovskiy | 43c3a4a | 2012-06-22 14:53:30 +0000 | [diff] [blame] | 3173 | TheSubsets = SI.TheSubsets; |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 3174 | SubclassOptionalData = SI.SubclassOptionalData; |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3175 | } |
| 3176 | |
Gordon Henriksen | afba8fe | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 3177 | SwitchInst::~SwitchInst() { |
Jay Foad | 1ed26ac | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 3178 | dropHungoffUses(); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3179 | } |
| 3180 | |
| 3181 | |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3182 | /// addCase - Add an entry to the switch instruction... |
| 3183 | /// |
Chris Lattner | d1a3260 | 2005-02-24 05:32:09 +0000 | [diff] [blame] | 3184 | void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { |
Stepan Dyatkovskiy | 0aa32d5 | 2012-05-29 12:26:47 +0000 | [diff] [blame] | 3185 | IntegersSubsetToBB Mapping; |
Stepan Dyatkovskiy | 484fc93 | 2012-05-28 12:39:09 +0000 | [diff] [blame] | 3186 | |
| 3187 | // FIXME: Currently we work with ConstantInt based cases. |
| 3188 | // So inititalize IntItem container directly from ConstantInt. |
Stepan Dyatkovskiy | 0aa32d5 | 2012-05-29 12:26:47 +0000 | [diff] [blame] | 3189 | Mapping.add(IntItem::fromConstantInt(OnVal)); |
| 3190 | IntegersSubset CaseRanges = Mapping.getCase(); |
| 3191 | addCase(CaseRanges, Dest); |
Stepan Dyatkovskiy | 1cce5bf | 2012-05-12 10:48:17 +0000 | [diff] [blame] | 3192 | } |
| 3193 | |
Stepan Dyatkovskiy | 0aa32d5 | 2012-05-29 12:26:47 +0000 | [diff] [blame] | 3194 | void SwitchInst::addCase(IntegersSubset& OnVal, BasicBlock *Dest) { |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3195 | unsigned NewCaseIdx = getNumCases(); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3196 | unsigned OpNo = NumOperands; |
| 3197 | if (OpNo+2 > ReservedSpace) |
Jay Foad | 8891ed7 | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3198 | growOperands(); // Get more space! |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3199 | // Initialize some new operands. |
Chris Lattner | 1f377fc | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 3200 | assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3201 | NumOperands = OpNo+2; |
Stepan Dyatkovskiy | 43c3a4a | 2012-06-22 14:53:30 +0000 | [diff] [blame] | 3202 | |
| 3203 | SubsetsIt TheSubsetsIt = TheSubsets.insert(TheSubsets.end(), OnVal); |
| 3204 | |
| 3205 | CaseIt Case(this, NewCaseIdx, TheSubsetsIt); |
| 3206 | Case.updateCaseValueOperand(OnVal); |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 3207 | Case.setSuccessor(Dest); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3208 | } |
| 3209 | |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3210 | /// removeCase - This method removes the specified case and its successor |
| 3211 | /// from the switch instruction. |
Stepan Dyatkovskiy | 43c3a4a | 2012-06-22 14:53:30 +0000 | [diff] [blame] | 3212 | void SwitchInst::removeCase(CaseIt& i) { |
Stepan Dyatkovskiy | c10fa6c | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 3213 | unsigned idx = i.getCaseIndex(); |
| 3214 | |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3215 | assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!"); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3216 | |
| 3217 | unsigned NumOps = getNumOperands(); |
| 3218 | Use *OL = OperandList; |
| 3219 | |
Jay Foad | 0faa609 | 2011-02-01 09:22:34 +0000 | [diff] [blame] | 3220 | // Overwrite this case with the end of the list. |
Stepan Dyatkovskiy | 2447312 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3221 | if (2 + (idx + 1) * 2 != NumOps) { |
| 3222 | OL[2 + idx * 2] = OL[NumOps - 2]; |
| 3223 | OL[2 + idx * 2 + 1] = OL[NumOps - 1]; |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3224 | } |
| 3225 | |
| 3226 | // Nuke the last value. |
| 3227 | OL[NumOps-2].set(0); |
| 3228 | OL[NumOps-2+1].set(0); |
Stepan Dyatkovskiy | 43c3a4a | 2012-06-22 14:53:30 +0000 | [diff] [blame] | 3229 | |
| 3230 | // Do the same with TheCases collection: |
| 3231 | if (i.SubsetIt != --TheSubsets.end()) { |
| 3232 | *i.SubsetIt = TheSubsets.back(); |
| 3233 | TheSubsets.pop_back(); |
| 3234 | } else { |
| 3235 | TheSubsets.pop_back(); |
| 3236 | i.SubsetIt = TheSubsets.end(); |
| 3237 | } |
| 3238 | |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3239 | NumOperands = NumOps-2; |
| 3240 | } |
| 3241 | |
Jay Foad | 8891ed7 | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3242 | /// growOperands - grow operands - This grows the operand list in response |
| 3243 | /// to a push_back style of operation. This grows the number of ops by 3 times. |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3244 | /// |
Jay Foad | 8891ed7 | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3245 | void SwitchInst::growOperands() { |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3246 | unsigned e = getNumOperands(); |
Jay Foad | 8891ed7 | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3247 | unsigned NumOps = e*3; |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3248 | |
| 3249 | ReservedSpace = NumOps; |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3250 | Use *NewOps = allocHungoffUses(NumOps); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3251 | Use *OldOps = OperandList; |
Gabor Greif | efe6536 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3252 | for (unsigned i = 0; i != e; ++i) { |
Gabor Greif | 6c80c38 | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3253 | NewOps[i] = OldOps[i]; |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3254 | } |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3255 | OperandList = NewOps; |
Jay Foad | 1ed26ac | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 3256 | Use::zap(OldOps, OldOps + e, true); |
Chris Lattner | b12261a | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
| 3259 | |
| 3260 | BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const { |
| 3261 | return getSuccessor(idx); |
| 3262 | } |
| 3263 | unsigned SwitchInst::getNumSuccessorsV() const { |
| 3264 | return getNumSuccessors(); |
| 3265 | } |
| 3266 | void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 3267 | setSuccessor(idx, B); |
Alkis Evlogimenos | 91366a8 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3268 | } |
Chris Lattner | 795948a | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3269 | |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3270 | //===----------------------------------------------------------------------===// |
Jay Foad | 1ed26ac | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 3271 | // IndirectBrInst Implementation |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3272 | //===----------------------------------------------------------------------===// |
| 3273 | |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3274 | void IndirectBrInst::init(Value *Address, unsigned NumDests) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3275 | assert(Address && Address->getType()->isPointerTy() && |
Chris Lattner | a4c206f | 2009-10-29 05:53:32 +0000 | [diff] [blame] | 3276 | "Address of indirectbr must be a pointer"); |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3277 | ReservedSpace = 1+NumDests; |
| 3278 | NumOperands = 1; |
| 3279 | OperandList = allocHungoffUses(ReservedSpace); |
| 3280 | |
| 3281 | OperandList[0] = Address; |
| 3282 | } |
| 3283 | |
| 3284 | |
Jay Foad | 8891ed7 | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3285 | /// growOperands - grow operands - This grows the operand list in response |
| 3286 | /// to a push_back style of operation. This grows the number of ops by 2 times. |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3287 | /// |
Jay Foad | 8891ed7 | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3288 | void IndirectBrInst::growOperands() { |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3289 | unsigned e = getNumOperands(); |
Jay Foad | 8891ed7 | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3290 | unsigned NumOps = e*2; |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3291 | |
| 3292 | ReservedSpace = NumOps; |
| 3293 | Use *NewOps = allocHungoffUses(NumOps); |
| 3294 | Use *OldOps = OperandList; |
| 3295 | for (unsigned i = 0; i != e; ++i) |
| 3296 | NewOps[i] = OldOps[i]; |
| 3297 | OperandList = NewOps; |
Jay Foad | 1ed26ac | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 3298 | Use::zap(OldOps, OldOps + e, true); |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3299 | } |
| 3300 | |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3301 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, |
| 3302 | Instruction *InsertBefore) |
| 3303 | : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr, |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3304 | 0, 0, InsertBefore) { |
| 3305 | init(Address, NumCases); |
| 3306 | } |
| 3307 | |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3308 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, |
| 3309 | BasicBlock *InsertAtEnd) |
| 3310 | : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr, |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3311 | 0, 0, InsertAtEnd) { |
| 3312 | init(Address, NumCases); |
| 3313 | } |
| 3314 | |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3315 | IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI) |
| 3316 | : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr, |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3317 | allocHungoffUses(IBI.getNumOperands()), |
| 3318 | IBI.getNumOperands()) { |
| 3319 | Use *OL = OperandList, *InOL = IBI.OperandList; |
| 3320 | for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i) |
| 3321 | OL[i] = InOL[i]; |
| 3322 | SubclassOptionalData = IBI.SubclassOptionalData; |
| 3323 | } |
| 3324 | |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3325 | IndirectBrInst::~IndirectBrInst() { |
Jay Foad | 1ed26ac | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 3326 | dropHungoffUses(); |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
| 3329 | /// addDestination - Add a destination. |
| 3330 | /// |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3331 | void IndirectBrInst::addDestination(BasicBlock *DestBB) { |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3332 | unsigned OpNo = NumOperands; |
| 3333 | if (OpNo+1 > ReservedSpace) |
Jay Foad | 8891ed7 | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3334 | growOperands(); // Get more space! |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3335 | // Initialize some new operands. |
| 3336 | assert(OpNo < ReservedSpace && "Growing didn't work!"); |
| 3337 | NumOperands = OpNo+1; |
| 3338 | OperandList[OpNo] = DestBB; |
| 3339 | } |
| 3340 | |
| 3341 | /// removeDestination - This method removes the specified successor from the |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3342 | /// indirectbr instruction. |
| 3343 | void IndirectBrInst::removeDestination(unsigned idx) { |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3344 | assert(idx < getNumOperands()-1 && "Successor index out of range!"); |
| 3345 | |
| 3346 | unsigned NumOps = getNumOperands(); |
| 3347 | Use *OL = OperandList; |
| 3348 | |
| 3349 | // Replace this value with the last one. |
| 3350 | OL[idx+1] = OL[NumOps-1]; |
| 3351 | |
| 3352 | // Nuke the last value. |
| 3353 | OL[NumOps-1].set(0); |
| 3354 | NumOperands = NumOps-1; |
| 3355 | } |
| 3356 | |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3357 | BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const { |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3358 | return getSuccessor(idx); |
| 3359 | } |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3360 | unsigned IndirectBrInst::getNumSuccessorsV() const { |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3361 | return getNumSuccessors(); |
| 3362 | } |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3363 | void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3364 | setSuccessor(idx, B); |
| 3365 | } |
| 3366 | |
| 3367 | //===----------------------------------------------------------------------===// |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3368 | // clone_impl() implementations |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3369 | //===----------------------------------------------------------------------===// |
| 3370 | |
Chris Lattner | 795948a | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3371 | // Define these methods here so vtables don't get emitted into every translation |
| 3372 | // unit that uses these classes. |
| 3373 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3374 | GetElementPtrInst *GetElementPtrInst::clone_impl() const { |
| 3375 | return new (getNumOperands()) GetElementPtrInst(*this); |
Chris Lattner | 795948a | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3376 | } |
| 3377 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3378 | BinaryOperator *BinaryOperator::clone_impl() const { |
| 3379 | return Create(getOpcode(), Op<0>(), Op<1>()); |
Chris Lattner | 795948a | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3380 | } |
| 3381 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3382 | FCmpInst* FCmpInst::clone_impl() const { |
| 3383 | return new FCmpInst(getPredicate(), Op<0>(), Op<1>()); |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3384 | } |
| 3385 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3386 | ICmpInst* ICmpInst::clone_impl() const { |
| 3387 | return new ICmpInst(getPredicate(), Op<0>(), Op<1>()); |
Dan Gohman | e456994 | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3390 | ExtractValueInst *ExtractValueInst::clone_impl() const { |
| 3391 | return new ExtractValueInst(*this); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3392 | } |
| 3393 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3394 | InsertValueInst *InsertValueInst::clone_impl() const { |
| 3395 | return new InsertValueInst(*this); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3396 | } |
| 3397 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3398 | AllocaInst *AllocaInst::clone_impl() const { |
| 3399 | return new AllocaInst(getAllocatedType(), |
| 3400 | (Value*)getOperand(0), |
| 3401 | getAlignment()); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3402 | } |
| 3403 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3404 | LoadInst *LoadInst::clone_impl() const { |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 3405 | return new LoadInst(getOperand(0), Twine(), isVolatile(), |
| 3406 | getAlignment(), getOrdering(), getSynchScope()); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3409 | StoreInst *StoreInst::clone_impl() const { |
Eli Friedman | 1cc5e38 | 2011-08-10 17:39:11 +0000 | [diff] [blame] | 3410 | return new StoreInst(getOperand(0), getOperand(1), isVolatile(), |
Eli Friedman | 21006d4 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 3411 | getAlignment(), getOrdering(), getSynchScope()); |
| 3412 | |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3413 | } |
| 3414 | |
Eli Friedman | ff03048 | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3415 | AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const { |
| 3416 | AtomicCmpXchgInst *Result = |
| 3417 | new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2), |
| 3418 | getOrdering(), getSynchScope()); |
| 3419 | Result->setVolatile(isVolatile()); |
| 3420 | return Result; |
| 3421 | } |
| 3422 | |
| 3423 | AtomicRMWInst *AtomicRMWInst::clone_impl() const { |
| 3424 | AtomicRMWInst *Result = |
| 3425 | new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1), |
| 3426 | getOrdering(), getSynchScope()); |
| 3427 | Result->setVolatile(isVolatile()); |
| 3428 | return Result; |
| 3429 | } |
| 3430 | |
Eli Friedman | 47f3513 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 3431 | FenceInst *FenceInst::clone_impl() const { |
| 3432 | return new FenceInst(getContext(), getOrdering(), getSynchScope()); |
| 3433 | } |
| 3434 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3435 | TruncInst *TruncInst::clone_impl() const { |
| 3436 | return new TruncInst(getOperand(0), getType()); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3437 | } |
| 3438 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3439 | ZExtInst *ZExtInst::clone_impl() const { |
| 3440 | return new ZExtInst(getOperand(0), getType()); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3441 | } |
| 3442 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3443 | SExtInst *SExtInst::clone_impl() const { |
| 3444 | return new SExtInst(getOperand(0), getType()); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3445 | } |
| 3446 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3447 | FPTruncInst *FPTruncInst::clone_impl() const { |
| 3448 | return new FPTruncInst(getOperand(0), getType()); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3449 | } |
| 3450 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3451 | FPExtInst *FPExtInst::clone_impl() const { |
| 3452 | return new FPExtInst(getOperand(0), getType()); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3453 | } |
| 3454 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3455 | UIToFPInst *UIToFPInst::clone_impl() const { |
| 3456 | return new UIToFPInst(getOperand(0), getType()); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3457 | } |
| 3458 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3459 | SIToFPInst *SIToFPInst::clone_impl() const { |
| 3460 | return new SIToFPInst(getOperand(0), getType()); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3461 | } |
| 3462 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3463 | FPToUIInst *FPToUIInst::clone_impl() const { |
| 3464 | return new FPToUIInst(getOperand(0), getType()); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3465 | } |
| 3466 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3467 | FPToSIInst *FPToSIInst::clone_impl() const { |
| 3468 | return new FPToSIInst(getOperand(0), getType()); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3469 | } |
| 3470 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3471 | PtrToIntInst *PtrToIntInst::clone_impl() const { |
| 3472 | return new PtrToIntInst(getOperand(0), getType()); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3473 | } |
| 3474 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3475 | IntToPtrInst *IntToPtrInst::clone_impl() const { |
| 3476 | return new IntToPtrInst(getOperand(0), getType()); |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3477 | } |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3478 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3479 | BitCastInst *BitCastInst::clone_impl() const { |
| 3480 | return new BitCastInst(getOperand(0), getType()); |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3481 | } |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3482 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3483 | CallInst *CallInst::clone_impl() const { |
| 3484 | return new(getNumOperands()) CallInst(*this); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3485 | } |
| 3486 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3487 | SelectInst *SelectInst::clone_impl() const { |
| 3488 | return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2)); |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3489 | } |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3490 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3491 | VAArgInst *VAArgInst::clone_impl() const { |
| 3492 | return new VAArgInst(getOperand(0), getType()); |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3493 | } |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3494 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3495 | ExtractElementInst *ExtractElementInst::clone_impl() const { |
| 3496 | return ExtractElementInst::Create(getOperand(0), getOperand(1)); |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3497 | } |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3498 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3499 | InsertElementInst *InsertElementInst::clone_impl() const { |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 3500 | return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2)); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3501 | } |
| 3502 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3503 | ShuffleVectorInst *ShuffleVectorInst::clone_impl() const { |
Chris Lattner | 83694a9 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 3504 | return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2)); |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3505 | } |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3506 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3507 | PHINode *PHINode::clone_impl() const { |
| 3508 | return new PHINode(*this); |
| 3509 | } |
| 3510 | |
Bill Wendling | e6e8826 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 3511 | LandingPadInst *LandingPadInst::clone_impl() const { |
| 3512 | return new LandingPadInst(*this); |
| 3513 | } |
| 3514 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3515 | ReturnInst *ReturnInst::clone_impl() const { |
| 3516 | return new(getNumOperands()) ReturnInst(*this); |
| 3517 | } |
| 3518 | |
| 3519 | BranchInst *BranchInst::clone_impl() const { |
Jay Foad | 8e3914d | 2011-01-07 20:29:02 +0000 | [diff] [blame] | 3520 | return new(getNumOperands()) BranchInst(*this); |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3521 | } |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3522 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3523 | SwitchInst *SwitchInst::clone_impl() const { |
| 3524 | return new SwitchInst(*this); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3525 | } |
| 3526 | |
Chris Lattner | ab21db7 | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3527 | IndirectBrInst *IndirectBrInst::clone_impl() const { |
| 3528 | return new IndirectBrInst(*this); |
Chris Lattner | f9be95f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3532 | InvokeInst *InvokeInst::clone_impl() const { |
| 3533 | return new(getNumOperands()) InvokeInst(*this); |
Gabor Greif | b1dbcd8 | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3534 | } |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3535 | |
Bill Wendling | dccc03b | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 3536 | ResumeInst *ResumeInst::clone_impl() const { |
| 3537 | return new(1) ResumeInst(*this); |
| 3538 | } |
| 3539 | |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3540 | UnreachableInst *UnreachableInst::clone_impl() const { |
Nick Lewycky | 6776064 | 2009-09-27 07:38:41 +0000 | [diff] [blame] | 3541 | LLVMContext &Context = getContext(); |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3542 | return new UnreachableInst(Context); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3543 | } |