Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1 | //===-- Instructions.cpp - Implement the LLVM instructions ----------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | afdb3de | 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 | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 15 | #include "llvm/IR/Instructions.h" |
Devang Patel | add5865 | 2009-09-23 18:32:25 +0000 | [diff] [blame] | 16 | #include "LLVMContextImpl.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 17 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 8cd041e | 2014-03-04 12:24:34 +0000 | [diff] [blame] | 18 | #include "llvm/IR/ConstantRange.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Constants.h" |
| 20 | #include "llvm/IR/DataLayout.h" |
| 21 | #include "llvm/IR/DerivedTypes.h" |
| 22 | #include "llvm/IR/Function.h" |
| 23 | #include "llvm/IR/Module.h" |
| 24 | #include "llvm/IR/Operator.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ErrorHandling.h" |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 26 | #include "llvm/Support/MathExtras.h" |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
| 30 | // CallSite Class |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
Gabor Greif | a2fbc0a | 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 | 638c823 | 2010-08-05 21:25:49 +0000 | [diff] [blame] | 36 | ? cast<CallInst>(II)->op_end() - 1 // Skip Callee |
Gabor Greif | 6d67395 | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 37 | : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Gordon Henriksen | 14a5569 | 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 | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 48 | //===----------------------------------------------------------------------===// |
| 49 | // UnaryInstruction Class |
| 50 | //===----------------------------------------------------------------------===// |
| 51 | |
Gordon Henriksen | 14a5569 | 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 | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 56 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8810795 | 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"; |
David Majnemer | b611e3f | 2015-08-14 05:09:07 +0000 | [diff] [blame] | 65 | |
| 66 | if (Op1->getType()->isTokenTy()) |
| 67 | return "select values cannot have token type"; |
| 68 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 69 | if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) { |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 70 | // Vector select. |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 71 | if (VT->getElementType() != Type::getInt1Ty(Op0->getContext())) |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 72 | return "vector select condition element type must be i1"; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 73 | VectorType *ET = dyn_cast<VectorType>(Op1->getType()); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 74 | if (!ET) |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 75 | return "selected values for vector select must be vectors"; |
| 76 | if (ET->getNumElements() != VT->getNumElements()) |
| 77 | return "vector select requires selected vectors to have " |
| 78 | "the same vector length as select condition"; |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 79 | } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) { |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 80 | return "select condition must be i1 or <n x i1>"; |
| 81 | } |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 82 | return nullptr; |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
| 86 | //===----------------------------------------------------------------------===// |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 87 | // PHINode Class |
| 88 | //===----------------------------------------------------------------------===// |
| 89 | |
| 90 | PHINode::PHINode(const PHINode &PN) |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 91 | : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()), |
| 92 | ReservedSpace(PN.getNumOperands()) { |
| 93 | allocHungoffUses(PN.getNumOperands()); |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 94 | std::copy(PN.op_begin(), PN.op_end(), op_begin()); |
| 95 | std::copy(PN.block_begin(), PN.block_end(), block_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 96 | SubclassOptionalData = PN.SubclassOptionalData; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 99 | // removeIncomingValue - Remove an incoming value. This is useful if a |
| 100 | // predecessor basic block is deleted. |
| 101 | Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 102 | Value *Removed = getIncomingValue(Idx); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 103 | |
| 104 | // Move everything after this operand down. |
| 105 | // |
| 106 | // FIXME: we could just swap with the end of the list, then erase. However, |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 107 | // clients might not expect this to happen. The code as it is thrashes the |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 108 | // use/def lists, which is kinda lame. |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 109 | std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx); |
| 110 | std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 111 | |
| 112 | // Nuke the last value. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 113 | Op<-1>().set(nullptr); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 114 | setNumHungOffUseOperands(getNumOperands() - 1); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 115 | |
| 116 | // If the PHI node is dead, because it has zero entries, nuke it now. |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 117 | if (getNumOperands() == 0 && DeletePHIIfEmpty) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 118 | // If anyone is using this PHI, make them use a dummy value instead... |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 119 | replaceAllUsesWith(UndefValue::get(getType())); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 120 | eraseFromParent(); |
| 121 | } |
| 122 | return Removed; |
| 123 | } |
| 124 | |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 125 | /// growOperands - grow operands - This grows the operand list in response |
| 126 | /// to a push_back style of operation. This grows the number of ops by 1.5 |
| 127 | /// times. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 128 | /// |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 129 | void PHINode::growOperands() { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 130 | unsigned e = getNumOperands(); |
Jay Foad | 61ea0e4 | 2011-06-23 09:09:15 +0000 | [diff] [blame] | 131 | unsigned NumOps = e + e / 2; |
| 132 | if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common. |
| 133 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 134 | ReservedSpace = NumOps; |
Pete Cooper | 93f9ff5 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 135 | growHungoffUses(ReservedSpace, /* IsPhi */ true); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 138 | /// hasConstantValue - If the specified PHI node always merges together the same |
| 139 | /// value, return the value, otherwise return null. |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 140 | Value *PHINode::hasConstantValue() const { |
| 141 | // Exploit the fact that phi nodes always have at least one entry. |
| 142 | Value *ConstantValue = getIncomingValue(0); |
| 143 | for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i) |
Nuno Lopes | 90c76df | 2012-07-03 17:10:28 +0000 | [diff] [blame] | 144 | if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) { |
| 145 | if (ConstantValue != this) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 146 | return nullptr; // Incoming values not all the same. |
Nuno Lopes | 90c76df | 2012-07-03 17:10:28 +0000 | [diff] [blame] | 147 | // The case where the first value is this PHI. |
| 148 | ConstantValue = getIncomingValue(i); |
| 149 | } |
Nuno Lopes | 0d44a50 | 2012-07-03 21:15:40 +0000 | [diff] [blame] | 150 | if (ConstantValue == this) |
| 151 | return UndefValue::get(getType()); |
Duncan Sands | 7412f6e | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 152 | return ConstantValue; |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 155 | //===----------------------------------------------------------------------===// |
| 156 | // LandingPadInst Implementation |
| 157 | //===----------------------------------------------------------------------===// |
| 158 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 159 | LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues, |
| 160 | const Twine &NameStr, Instruction *InsertBefore) |
| 161 | : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) { |
| 162 | init(NumReservedValues, NameStr); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 163 | } |
| 164 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 165 | LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues, |
| 166 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
| 167 | : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) { |
| 168 | init(NumReservedValues, NameStr); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | LandingPadInst::LandingPadInst(const LandingPadInst &LP) |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 172 | : Instruction(LP.getType(), Instruction::LandingPad, nullptr, |
| 173 | LP.getNumOperands()), |
| 174 | ReservedSpace(LP.getNumOperands()) { |
| 175 | allocHungoffUses(LP.getNumOperands()); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 176 | Use *OL = getOperandList(); |
| 177 | const Use *InOL = LP.getOperandList(); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 178 | for (unsigned I = 0, E = ReservedSpace; I != E; ++I) |
| 179 | OL[I] = InOL[I]; |
| 180 | |
| 181 | setCleanup(LP.isCleanup()); |
| 182 | } |
| 183 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 184 | LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses, |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 185 | const Twine &NameStr, |
| 186 | Instruction *InsertBefore) { |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 187 | return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 188 | } |
| 189 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 190 | LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses, |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 191 | const Twine &NameStr, |
| 192 | BasicBlock *InsertAtEnd) { |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 193 | return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 194 | } |
| 195 | |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 196 | void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) { |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 197 | ReservedSpace = NumReservedValues; |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 198 | setNumHungOffUseOperands(0); |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 199 | allocHungoffUses(ReservedSpace); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 200 | setName(NameStr); |
| 201 | setCleanup(false); |
| 202 | } |
| 203 | |
| 204 | /// growOperands - grow operands - This grows the operand list in response to a |
| 205 | /// push_back style of operation. This grows the number of ops by 2 times. |
| 206 | void LandingPadInst::growOperands(unsigned Size) { |
| 207 | unsigned e = getNumOperands(); |
| 208 | if (ReservedSpace >= e + Size) return; |
David Majnemer | 7fddecc | 2015-06-17 20:52:32 +0000 | [diff] [blame] | 209 | ReservedSpace = (std::max(e, 1U) + Size / 2) * 2; |
Pete Cooper | 93f9ff5 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 210 | growHungoffUses(ReservedSpace); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Rafael Espindola | 4dc5dfc | 2014-06-04 18:51:31 +0000 | [diff] [blame] | 213 | void LandingPadInst::addClause(Constant *Val) { |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 214 | unsigned OpNo = getNumOperands(); |
| 215 | growOperands(1); |
| 216 | assert(OpNo < ReservedSpace && "Growing didn't work!"); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 217 | setNumHungOffUseOperands(getNumOperands() + 1); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 218 | getOperandList()[OpNo] = Val; |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 219 | } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 220 | |
| 221 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 222 | // CallInst Implementation |
| 223 | //===----------------------------------------------------------------------===// |
| 224 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 225 | CallInst::~CallInst() { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 226 | } |
| 227 | |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 228 | void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args, |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame^] | 229 | ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) { |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 230 | this->FTy = FTy; |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame^] | 231 | assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 && |
| 232 | "NumOperands not set up?"); |
Gabor Greif | 6d67395 | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 233 | Op<-1>() = Func; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 234 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 235 | #ifndef NDEBUG |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 236 | assert((Args.size() == FTy->getNumParams() || |
| 237 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 238 | "Calling a function with bad signature!"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 239 | |
| 240 | for (unsigned i = 0; i != Args.size(); ++i) |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 241 | assert((i >= FTy->getNumParams() || |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 242 | FTy->getParamType(i) == Args[i]->getType()) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 243 | "Calling a function with a bad signature!"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 244 | #endif |
| 245 | |
| 246 | std::copy(Args.begin(), Args.end(), op_begin()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame^] | 247 | |
| 248 | auto It = populateBundleOperandInfos(Bundles, Args.size()); |
| 249 | (void)It; |
| 250 | assert(It + 1 == op_end() && "Should add up!"); |
| 251 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 252 | setName(NameStr); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 255 | void CallInst::init(Value *Func, const Twine &NameStr) { |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 256 | FTy = |
| 257 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 258 | assert(getNumOperands() == 1 && "NumOperands not set up?"); |
Gabor Greif | 6d67395 | 2010-07-16 09:38:02 +0000 | [diff] [blame] | 259 | Op<-1>() = Func; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 260 | |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 261 | assert(FTy->getNumParams() == 0 && "Calling a function with bad signature"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 262 | |
| 263 | setName(NameStr); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 266 | CallInst::CallInst(Value *Func, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 267 | Instruction *InsertBefore) |
| 268 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 269 | ->getElementType())->getReturnType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 270 | Instruction::Call, |
| 271 | OperandTraits<CallInst>::op_end(this) - 1, |
| 272 | 1, InsertBefore) { |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 273 | init(Func, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 276 | CallInst::CallInst(Value *Func, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 277 | BasicBlock *InsertAtEnd) |
| 278 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 279 | ->getElementType())->getReturnType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 280 | Instruction::Call, |
| 281 | OperandTraits<CallInst>::op_end(this) - 1, |
| 282 | 1, InsertAtEnd) { |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 283 | init(Func, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 286 | CallInst::CallInst(const CallInst &CI) |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 287 | : Instruction(CI.getType(), Instruction::Call, |
| 288 | OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(), |
| 289 | CI.getNumOperands()), |
| 290 | AttributeList(CI.AttributeList), FTy(CI.FTy) { |
Reid Kleckner | 118e1bf | 2014-05-06 20:08:20 +0000 | [diff] [blame] | 291 | setTailCallKind(CI.getTailCallKind()); |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 292 | setCallingConv(CI.getCallingConv()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame^] | 293 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 294 | std::copy(CI.op_begin(), CI.op_end(), op_begin()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame^] | 295 | std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(), |
| 296 | bundle_op_info_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 297 | SubclassOptionalData = CI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Peter Collingbourne | 1b97a9c | 2013-03-02 01:20:18 +0000 | [diff] [blame] | 300 | void CallInst::addAttribute(unsigned i, Attribute::AttrKind attr) { |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 301 | AttributeSet PAL = getAttributes(); |
Peter Collingbourne | 1b97a9c | 2013-03-02 01:20:18 +0000 | [diff] [blame] | 302 | PAL = PAL.addAttribute(getContext(), i, attr); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 303 | setAttributes(PAL); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Akira Hatanaka | 5c40eeab | 2015-07-02 22:08:48 +0000 | [diff] [blame] | 306 | void CallInst::addAttribute(unsigned i, StringRef Kind, StringRef Value) { |
| 307 | AttributeSet PAL = getAttributes(); |
| 308 | PAL = PAL.addAttribute(getContext(), i, Kind, Value); |
| 309 | setAttributes(PAL); |
| 310 | } |
| 311 | |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 312 | void CallInst::removeAttribute(unsigned i, Attribute attr) { |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 313 | AttributeSet PAL = getAttributes(); |
Bill Wendling | 430fa9b | 2013-01-23 00:45:55 +0000 | [diff] [blame] | 314 | AttrBuilder B(attr); |
| 315 | LLVMContext &Context = getContext(); |
| 316 | PAL = PAL.removeAttributes(Context, i, |
| 317 | AttributeSet::get(Context, i, B)); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 318 | setAttributes(PAL); |
Duncan Sands | 78c8872 | 2008-07-08 08:38:44 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Ramkumar Ramachandra | 8fcb498 | 2015-02-14 19:37:54 +0000 | [diff] [blame] | 321 | void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) { |
| 322 | AttributeSet PAL = getAttributes(); |
| 323 | PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes); |
| 324 | setAttributes(PAL); |
| 325 | } |
| 326 | |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 327 | void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) { |
| 328 | AttributeSet PAL = getAttributes(); |
| 329 | PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes); |
| 330 | setAttributes(PAL); |
| 331 | } |
| 332 | |
Bill Wendling | c79e42c | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 333 | bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const { |
Bill Wendling | 749a43d | 2012-12-30 13:50:49 +0000 | [diff] [blame] | 334 | if (AttributeList.hasAttribute(i, A)) |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 335 | return true; |
| 336 | if (const Function *F = getCalledFunction()) |
Bill Wendling | 94dcaf8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 337 | return F->getAttributes().hasAttribute(i, A); |
Bill Wendling | daf8e38 | 2012-10-04 07:18:12 +0000 | [diff] [blame] | 338 | return false; |
| 339 | } |
| 340 | |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 341 | /// IsConstantOne - Return true only if val is constant int 1 |
| 342 | static bool IsConstantOne(Value *val) { |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 343 | assert(val && "IsConstantOne does not work with nullptr val"); |
Matt Arsenault | 6941785 | 2014-09-15 17:56:51 +0000 | [diff] [blame] | 344 | const ConstantInt *CVal = dyn_cast<ConstantInt>(val); |
| 345 | return CVal && CVal->isOne(); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 348 | static Instruction *createMalloc(Instruction *InsertBefore, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 349 | BasicBlock *InsertAtEnd, Type *IntPtrTy, |
| 350 | Type *AllocTy, Value *AllocSize, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 351 | Value *ArraySize, Function *MallocF, |
| 352 | const Twine &Name) { |
Benjamin Kramer | 4bf4e86 | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 353 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 354 | "createMalloc needs either InsertBefore or InsertAtEnd"); |
| 355 | |
| 356 | // malloc(type) becomes: |
| 357 | // bitcast (i8* malloc(typeSize)) to type* |
| 358 | // malloc(type, arraySize) becomes: |
| 359 | // bitcast (i8 *malloc(typeSize*arraySize)) to type* |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 360 | if (!ArraySize) |
| 361 | ArraySize = ConstantInt::get(IntPtrTy, 1); |
| 362 | else if (ArraySize->getType() != IntPtrTy) { |
| 363 | if (InsertBefore) |
Victor Hernandez | e04ed0c | 2009-11-07 00:36:50 +0000 | [diff] [blame] | 364 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, |
| 365 | "", InsertBefore); |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 366 | else |
Victor Hernandez | e04ed0c | 2009-11-07 00:36:50 +0000 | [diff] [blame] | 367 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, |
| 368 | "", InsertAtEnd); |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 369 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 370 | |
Benjamin Kramer | 4bf4e86 | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 371 | if (!IsConstantOne(ArraySize)) { |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 372 | if (IsConstantOne(AllocSize)) { |
| 373 | AllocSize = ArraySize; // Operand * 1 = Operand |
| 374 | } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) { |
| 375 | Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy, |
| 376 | false /*ZExt*/); |
| 377 | // Malloc arg is constant product of type size and array size |
| 378 | AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize)); |
| 379 | } else { |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 380 | // Multiply type size by the array size... |
| 381 | if (InsertBefore) |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 382 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, |
| 383 | "mallocsize", InsertBefore); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 384 | else |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 385 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, |
| 386 | "mallocsize", InsertAtEnd); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 387 | } |
Benjamin Kramer | 4bf4e86 | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 388 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 389 | |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 390 | assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size"); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 391 | // Create the call to Malloc. |
| 392 | BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; |
| 393 | Module* M = BB->getParent()->getParent(); |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 394 | Type *BPTy = Type::getInt8PtrTy(BB->getContext()); |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 395 | Value *MallocFunc = MallocF; |
| 396 | if (!MallocFunc) |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 397 | // prototype malloc as "void *malloc(size_t)" |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 398 | MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, nullptr); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 399 | PointerType *AllocPtrType = PointerType::getUnqual(AllocTy); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 400 | CallInst *MCall = nullptr; |
| 401 | Instruction *Result = nullptr; |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 402 | if (InsertBefore) { |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 403 | MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 404 | Result = MCall; |
| 405 | if (Result->getType() != AllocPtrType) |
| 406 | // Create a cast instruction to convert to the right type... |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 407 | Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore); |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 408 | } else { |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 409 | MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall"); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 410 | Result = MCall; |
| 411 | if (Result->getType() != AllocPtrType) { |
| 412 | InsertAtEnd->getInstList().push_back(MCall); |
| 413 | // Create a cast instruction to convert to the right type... |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 414 | Result = new BitCastInst(MCall, AllocPtrType, Name); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 415 | } |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 416 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 417 | MCall->setTailCall(); |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 418 | if (Function *F = dyn_cast<Function>(MallocFunc)) { |
| 419 | MCall->setCallingConv(F->getCallingConv()); |
| 420 | if (!F->doesNotAlias(0)) F->setDoesNotAlias(0); |
| 421 | } |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 422 | assert(!MCall->getType()->isVoidTy() && "Malloc has void return type"); |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 423 | |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 424 | return Result; |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /// CreateMalloc - Generate the IR for a call to malloc: |
| 428 | /// 1. Compute the malloc call's argument as the specified type's size, |
| 429 | /// possibly multiplied by the array size if the array size is not |
| 430 | /// constant 1. |
| 431 | /// 2. Call malloc with that argument. |
| 432 | /// 3. Bitcast the result of the malloc call to the specified type. |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 433 | Instruction *CallInst::CreateMalloc(Instruction *InsertBefore, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 434 | Type *IntPtrTy, Type *AllocTy, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 435 | Value *AllocSize, Value *ArraySize, |
Duncan Sands | 41b4a6b | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 436 | Function * MallocF, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 437 | const Twine &Name) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 438 | return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize, |
Chris Lattner | 601e390a | 2010-07-12 00:57:28 +0000 | [diff] [blame] | 439 | ArraySize, MallocF, Name); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /// CreateMalloc - Generate the IR for a call to malloc: |
| 443 | /// 1. Compute the malloc call's argument as the specified type's size, |
| 444 | /// possibly multiplied by the array size if the array size is not |
| 445 | /// constant 1. |
| 446 | /// 2. Call malloc with that argument. |
| 447 | /// 3. Bitcast the result of the malloc call to the specified type. |
| 448 | /// Note: This function does not add the bitcast to the basic block, that is the |
| 449 | /// responsibility of the caller. |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 450 | Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 451 | Type *IntPtrTy, Type *AllocTy, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 452 | Value *AllocSize, Value *ArraySize, |
| 453 | Function *MallocF, const Twine &Name) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 454 | return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize, |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 455 | ArraySize, MallocF, Name); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 456 | } |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 457 | |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 458 | static Instruction* createFree(Value* Source, Instruction *InsertBefore, |
| 459 | BasicBlock *InsertAtEnd) { |
| 460 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && |
| 461 | "createFree needs either InsertBefore or InsertAtEnd"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 462 | assert(Source->getType()->isPointerTy() && |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 463 | "Can not free something of nonpointer type!"); |
| 464 | |
| 465 | BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; |
| 466 | Module* M = BB->getParent()->getParent(); |
| 467 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 468 | Type *VoidTy = Type::getVoidTy(M->getContext()); |
| 469 | Type *IntPtrTy = Type::getInt8PtrTy(M->getContext()); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 470 | // prototype free as "void free(void*)" |
Reid Kleckner | 971c3ea | 2014-11-13 22:55:19 +0000 | [diff] [blame] | 471 | Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, nullptr); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 472 | CallInst* Result = nullptr; |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 473 | Value *PtrCast = Source; |
| 474 | if (InsertBefore) { |
| 475 | if (Source->getType() != IntPtrTy) |
| 476 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore); |
| 477 | Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore); |
| 478 | } else { |
| 479 | if (Source->getType() != IntPtrTy) |
| 480 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd); |
| 481 | Result = CallInst::Create(FreeFunc, PtrCast, ""); |
| 482 | } |
| 483 | Result->setTailCall(); |
Chris Lattner | 2156c22 | 2009-11-09 07:12:01 +0000 | [diff] [blame] | 484 | if (Function *F = dyn_cast<Function>(FreeFunc)) |
| 485 | Result->setCallingConv(F->getCallingConv()); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 486 | |
| 487 | return Result; |
| 488 | } |
| 489 | |
| 490 | /// CreateFree - Generate the IR for a call to the builtin free function. |
Chris Lattner | 601e390a | 2010-07-12 00:57:28 +0000 | [diff] [blame] | 491 | Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 492 | return createFree(Source, InsertBefore, nullptr); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /// CreateFree - Generate the IR for a call to the builtin free function. |
| 496 | /// Note: This function does not add the call to the basic block, that is the |
| 497 | /// responsibility of the caller. |
| 498 | Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 499 | Instruction* FreeCall = createFree(Source, nullptr, InsertAtEnd); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 500 | assert(FreeCall && "CreateFree did not create a CallInst"); |
| 501 | return FreeCall; |
| 502 | } |
| 503 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 504 | //===----------------------------------------------------------------------===// |
| 505 | // InvokeInst Implementation |
| 506 | //===----------------------------------------------------------------------===// |
| 507 | |
David Blaikie | 3e80709 | 2015-05-13 18:35:26 +0000 | [diff] [blame] | 508 | void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal, |
| 509 | BasicBlock *IfException, ArrayRef<Value *> Args, |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame^] | 510 | ArrayRef<OperandBundleDef> Bundles, |
David Blaikie | 3e80709 | 2015-05-13 18:35:26 +0000 | [diff] [blame] | 511 | const Twine &NameStr) { |
| 512 | this->FTy = FTy; |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 513 | |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame^] | 514 | assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) && |
| 515 | "NumOperands not set up?"); |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 516 | Op<-3>() = Fn; |
| 517 | Op<-2>() = IfNormal; |
| 518 | Op<-1>() = IfException; |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 519 | |
| 520 | #ifndef NDEBUG |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 521 | assert(((Args.size() == FTy->getNumParams()) || |
| 522 | (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && |
Gabor Greif | 668d700 | 2010-03-23 13:45:54 +0000 | [diff] [blame] | 523 | "Invoking a function with bad signature"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 524 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 525 | for (unsigned i = 0, e = Args.size(); i != e; i++) |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 526 | assert((i >= FTy->getNumParams() || |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 527 | FTy->getParamType(i) == Args[i]->getType()) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 528 | "Invoking a function with a bad signature!"); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 529 | #endif |
| 530 | |
| 531 | std::copy(Args.begin(), Args.end(), op_begin()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame^] | 532 | |
| 533 | auto It = populateBundleOperandInfos(Bundles, Args.size()); |
| 534 | (void)It; |
| 535 | assert(It + 3 == op_end() && "Should add up!"); |
| 536 | |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 537 | setName(NameStr); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 540 | InvokeInst::InvokeInst(const InvokeInst &II) |
David Blaikie | 348de69 | 2015-04-23 21:36:23 +0000 | [diff] [blame] | 541 | : TerminatorInst(II.getType(), Instruction::Invoke, |
| 542 | OperandTraits<InvokeInst>::op_end(this) - |
| 543 | II.getNumOperands(), |
| 544 | II.getNumOperands()), |
| 545 | AttributeList(II.AttributeList), FTy(II.FTy) { |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 546 | setCallingConv(II.getCallingConv()); |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 547 | std::copy(II.op_begin(), II.op_end(), op_begin()); |
Sanjoy Das | 9303c24 | 2015-09-24 19:14:18 +0000 | [diff] [blame^] | 548 | std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(), |
| 549 | bundle_op_info_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 550 | SubclassOptionalData = II.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 553 | BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const { |
| 554 | return getSuccessor(idx); |
| 555 | } |
| 556 | unsigned InvokeInst::getNumSuccessorsV() const { |
| 557 | return getNumSuccessors(); |
| 558 | } |
| 559 | void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 560 | return setSuccessor(idx, B); |
| 561 | } |
| 562 | |
Michael Gottesman | 41748d7 | 2013-06-27 00:25:01 +0000 | [diff] [blame] | 563 | bool InvokeInst::hasFnAttrImpl(Attribute::AttrKind A) const { |
Bill Wendling | 749a43d | 2012-12-30 13:50:49 +0000 | [diff] [blame] | 564 | if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A)) |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 565 | return true; |
| 566 | if (const Function *F = getCalledFunction()) |
Bill Wendling | 94dcaf8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 567 | return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A); |
Bill Wendling | 375eb1f | 2012-10-09 00:28:54 +0000 | [diff] [blame] | 568 | return false; |
| 569 | } |
| 570 | |
Bill Wendling | c79e42c | 2012-12-22 00:37:52 +0000 | [diff] [blame] | 571 | bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const { |
Bill Wendling | 749a43d | 2012-12-30 13:50:49 +0000 | [diff] [blame] | 572 | if (AttributeList.hasAttribute(i, A)) |
Bill Wendling | 8baa61d | 2012-10-03 17:54:26 +0000 | [diff] [blame] | 573 | return true; |
| 574 | if (const Function *F = getCalledFunction()) |
Bill Wendling | 94dcaf8 | 2012-12-30 12:45:13 +0000 | [diff] [blame] | 575 | return F->getAttributes().hasAttribute(i, A); |
Bill Wendling | daf8e38 | 2012-10-04 07:18:12 +0000 | [diff] [blame] | 576 | return false; |
| 577 | } |
| 578 | |
Peter Collingbourne | 1b97a9c | 2013-03-02 01:20:18 +0000 | [diff] [blame] | 579 | void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind attr) { |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 580 | AttributeSet PAL = getAttributes(); |
Peter Collingbourne | 1b97a9c | 2013-03-02 01:20:18 +0000 | [diff] [blame] | 581 | PAL = PAL.addAttribute(getContext(), i, attr); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 582 | setAttributes(PAL); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 585 | void InvokeInst::removeAttribute(unsigned i, Attribute attr) { |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 586 | AttributeSet PAL = getAttributes(); |
Bill Wendling | 430fa9b | 2013-01-23 00:45:55 +0000 | [diff] [blame] | 587 | AttrBuilder B(attr); |
| 588 | PAL = PAL.removeAttributes(getContext(), i, |
| 589 | AttributeSet::get(getContext(), i, B)); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 590 | setAttributes(PAL); |
Duncan Sands | aa31b92 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Ramkumar Ramachandra | 8fcb498 | 2015-02-14 19:37:54 +0000 | [diff] [blame] | 593 | void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) { |
| 594 | AttributeSet PAL = getAttributes(); |
| 595 | PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes); |
| 596 | setAttributes(PAL); |
| 597 | } |
| 598 | |
Sanjoy Das | 31ea6d1 | 2015-04-16 20:29:50 +0000 | [diff] [blame] | 599 | void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) { |
| 600 | AttributeSet PAL = getAttributes(); |
| 601 | PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes); |
| 602 | setAttributes(PAL); |
| 603 | } |
| 604 | |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 605 | LandingPadInst *InvokeInst::getLandingPadInst() const { |
| 606 | return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI()); |
| 607 | } |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 608 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 609 | //===----------------------------------------------------------------------===// |
| 610 | // ReturnInst Implementation |
| 611 | //===----------------------------------------------------------------------===// |
| 612 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 613 | ReturnInst::ReturnInst(const ReturnInst &RI) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 614 | : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 615 | OperandTraits<ReturnInst>::op_end(this) - |
| 616 | RI.getNumOperands(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 617 | RI.getNumOperands()) { |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 618 | if (RI.getNumOperands()) |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 619 | Op<0>() = RI.Op<0>(); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 620 | SubclassOptionalData = RI.SubclassOptionalData; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 623 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore) |
| 624 | : TerminatorInst(Type::getVoidTy(C), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 625 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, |
| 626 | InsertBefore) { |
Devang Patel | c38eb52 | 2008-02-26 18:49:29 +0000 | [diff] [blame] | 627 | if (retVal) |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 628 | Op<0>() = retVal; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 629 | } |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 630 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd) |
| 631 | : TerminatorInst(Type::getVoidTy(C), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 632 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, |
| 633 | InsertAtEnd) { |
Devang Patel | c38eb52 | 2008-02-26 18:49:29 +0000 | [diff] [blame] | 634 | if (retVal) |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 635 | Op<0>() = retVal; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 636 | } |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 637 | ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
| 638 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 639 | OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) { |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 642 | unsigned ReturnInst::getNumSuccessorsV() const { |
| 643 | return getNumSuccessors(); |
| 644 | } |
| 645 | |
Devang Patel | ae682fb | 2008-02-26 17:56:20 +0000 | [diff] [blame] | 646 | /// Out-of-line ReturnInst method, put here so the C++ compiler can choose to |
| 647 | /// emit the vtable for the class in this translation unit. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 648 | void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 649 | llvm_unreachable("ReturnInst has no successors!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 652 | BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 653 | llvm_unreachable("ReturnInst has no successors!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 656 | ReturnInst::~ReturnInst() { |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 657 | } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 658 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 659 | //===----------------------------------------------------------------------===// |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 660 | // ResumeInst Implementation |
| 661 | //===----------------------------------------------------------------------===// |
| 662 | |
| 663 | ResumeInst::ResumeInst(const ResumeInst &RI) |
| 664 | : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume, |
| 665 | OperandTraits<ResumeInst>::op_begin(this), 1) { |
| 666 | Op<0>() = RI.Op<0>(); |
| 667 | } |
| 668 | |
| 669 | ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore) |
| 670 | : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume, |
| 671 | OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) { |
| 672 | Op<0>() = Exn; |
| 673 | } |
| 674 | |
| 675 | ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd) |
| 676 | : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume, |
| 677 | OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) { |
| 678 | Op<0>() = Exn; |
| 679 | } |
| 680 | |
| 681 | unsigned ResumeInst::getNumSuccessorsV() const { |
| 682 | return getNumSuccessors(); |
| 683 | } |
| 684 | |
| 685 | void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
| 686 | llvm_unreachable("ResumeInst has no successors!"); |
| 687 | } |
| 688 | |
| 689 | BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const { |
| 690 | llvm_unreachable("ResumeInst has no successors!"); |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | //===----------------------------------------------------------------------===// |
Joseph Tremoulet | 9ce71f7 | 2015-09-03 09:09:43 +0000 | [diff] [blame] | 694 | // CleanupEndPadInst Implementation |
| 695 | //===----------------------------------------------------------------------===// |
| 696 | |
| 697 | CleanupEndPadInst::CleanupEndPadInst(const CleanupEndPadInst &CEPI) |
| 698 | : TerminatorInst(CEPI.getType(), Instruction::CleanupEndPad, |
| 699 | OperandTraits<CleanupEndPadInst>::op_end(this) - |
| 700 | CEPI.getNumOperands(), |
| 701 | CEPI.getNumOperands()) { |
| 702 | setInstructionSubclassData(CEPI.getSubclassDataFromInstruction()); |
| 703 | setCleanupPad(CEPI.getCleanupPad()); |
| 704 | if (BasicBlock *UnwindDest = CEPI.getUnwindDest()) |
| 705 | setUnwindDest(UnwindDest); |
| 706 | } |
| 707 | |
| 708 | void CleanupEndPadInst::init(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB) { |
| 709 | setCleanupPad(CleanupPad); |
| 710 | if (UnwindBB) { |
| 711 | setInstructionSubclassData(getSubclassDataFromInstruction() | 1); |
| 712 | setUnwindDest(UnwindBB); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | CleanupEndPadInst::CleanupEndPadInst(CleanupPadInst *CleanupPad, |
| 717 | BasicBlock *UnwindBB, unsigned Values, |
| 718 | Instruction *InsertBefore) |
| 719 | : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()), |
| 720 | Instruction::CleanupEndPad, |
| 721 | OperandTraits<CleanupEndPadInst>::op_end(this) - Values, |
| 722 | Values, InsertBefore) { |
| 723 | init(CleanupPad, UnwindBB); |
| 724 | } |
| 725 | |
| 726 | CleanupEndPadInst::CleanupEndPadInst(CleanupPadInst *CleanupPad, |
| 727 | BasicBlock *UnwindBB, unsigned Values, |
| 728 | BasicBlock *InsertAtEnd) |
| 729 | : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()), |
| 730 | Instruction::CleanupEndPad, |
| 731 | OperandTraits<CleanupEndPadInst>::op_end(this) - Values, |
| 732 | Values, InsertAtEnd) { |
| 733 | init(CleanupPad, UnwindBB); |
| 734 | } |
| 735 | |
| 736 | BasicBlock *CleanupEndPadInst::getSuccessorV(unsigned Idx) const { |
| 737 | assert(Idx == 0); |
| 738 | return getUnwindDest(); |
| 739 | } |
| 740 | unsigned CleanupEndPadInst::getNumSuccessorsV() const { |
| 741 | return getNumSuccessors(); |
| 742 | } |
| 743 | void CleanupEndPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) { |
| 744 | assert(Idx == 0); |
| 745 | setUnwindDest(B); |
| 746 | } |
| 747 | |
| 748 | //===----------------------------------------------------------------------===// |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 749 | // CleanupReturnInst Implementation |
| 750 | //===----------------------------------------------------------------------===// |
| 751 | |
| 752 | CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI) |
| 753 | : TerminatorInst(CRI.getType(), Instruction::CleanupRet, |
| 754 | OperandTraits<CleanupReturnInst>::op_end(this) - |
| 755 | CRI.getNumOperands(), |
| 756 | CRI.getNumOperands()) { |
David Majnemer | eb518bd | 2015-08-04 08:21:40 +0000 | [diff] [blame] | 757 | setInstructionSubclassData(CRI.getSubclassDataFromInstruction()); |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 758 | Op<-1>() = CRI.Op<-1>(); |
| 759 | if (CRI.hasUnwindDest()) |
| 760 | Op<-2>() = CRI.Op<-2>(); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 763 | void CleanupReturnInst::init(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB) { |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 764 | if (UnwindBB) |
| 765 | setInstructionSubclassData(getSubclassDataFromInstruction() | 1); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 766 | |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 767 | Op<-1>() = CleanupPad; |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 768 | if (UnwindBB) |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 769 | Op<-2>() = UnwindBB; |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 772 | CleanupReturnInst::CleanupReturnInst(CleanupPadInst *CleanupPad, |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 773 | BasicBlock *UnwindBB, unsigned Values, |
| 774 | Instruction *InsertBefore) |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 775 | : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()), |
| 776 | Instruction::CleanupRet, |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 777 | OperandTraits<CleanupReturnInst>::op_end(this) - Values, |
| 778 | Values, InsertBefore) { |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 779 | init(CleanupPad, UnwindBB); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 782 | CleanupReturnInst::CleanupReturnInst(CleanupPadInst *CleanupPad, |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 783 | BasicBlock *UnwindBB, unsigned Values, |
| 784 | BasicBlock *InsertAtEnd) |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 785 | : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()), |
| 786 | Instruction::CleanupRet, |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 787 | OperandTraits<CleanupReturnInst>::op_end(this) - Values, |
| 788 | Values, InsertAtEnd) { |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 789 | init(CleanupPad, UnwindBB); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | BasicBlock *CleanupReturnInst::getSuccessorV(unsigned Idx) const { |
| 793 | assert(Idx == 0); |
| 794 | return getUnwindDest(); |
| 795 | } |
| 796 | unsigned CleanupReturnInst::getNumSuccessorsV() const { |
| 797 | return getNumSuccessors(); |
| 798 | } |
| 799 | void CleanupReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) { |
| 800 | assert(Idx == 0); |
| 801 | setUnwindDest(B); |
| 802 | } |
| 803 | |
| 804 | //===----------------------------------------------------------------------===// |
| 805 | // CatchEndPadInst Implementation |
| 806 | //===----------------------------------------------------------------------===// |
| 807 | |
| 808 | CatchEndPadInst::CatchEndPadInst(const CatchEndPadInst &CRI) |
| 809 | : TerminatorInst(CRI.getType(), Instruction::CatchEndPad, |
| 810 | OperandTraits<CatchEndPadInst>::op_end(this) - |
| 811 | CRI.getNumOperands(), |
| 812 | CRI.getNumOperands()) { |
David Majnemer | eb518bd | 2015-08-04 08:21:40 +0000 | [diff] [blame] | 813 | setInstructionSubclassData(CRI.getSubclassDataFromInstruction()); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 814 | if (BasicBlock *UnwindDest = CRI.getUnwindDest()) |
| 815 | setUnwindDest(UnwindDest); |
| 816 | } |
| 817 | |
| 818 | void CatchEndPadInst::init(BasicBlock *UnwindBB) { |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 819 | if (UnwindBB) { |
| 820 | setInstructionSubclassData(getSubclassDataFromInstruction() | 1); |
| 821 | setUnwindDest(UnwindBB); |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | CatchEndPadInst::CatchEndPadInst(LLVMContext &C, BasicBlock *UnwindBB, |
David Majnemer | 5c73c94 | 2015-08-13 22:11:40 +0000 | [diff] [blame] | 826 | unsigned Values, Instruction *InsertBefore) |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 827 | : TerminatorInst(Type::getVoidTy(C), Instruction::CatchEndPad, |
| 828 | OperandTraits<CatchEndPadInst>::op_end(this) - Values, |
| 829 | Values, InsertBefore) { |
| 830 | init(UnwindBB); |
| 831 | } |
| 832 | |
| 833 | CatchEndPadInst::CatchEndPadInst(LLVMContext &C, BasicBlock *UnwindBB, |
David Majnemer | 5c73c94 | 2015-08-13 22:11:40 +0000 | [diff] [blame] | 834 | unsigned Values, BasicBlock *InsertAtEnd) |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 835 | : TerminatorInst(Type::getVoidTy(C), Instruction::CatchEndPad, |
| 836 | OperandTraits<CatchEndPadInst>::op_end(this) - Values, |
| 837 | Values, InsertAtEnd) { |
| 838 | init(UnwindBB); |
| 839 | } |
| 840 | |
| 841 | BasicBlock *CatchEndPadInst::getSuccessorV(unsigned Idx) const { |
| 842 | assert(Idx == 0); |
| 843 | return getUnwindDest(); |
| 844 | } |
| 845 | unsigned CatchEndPadInst::getNumSuccessorsV() const { |
| 846 | return getNumSuccessors(); |
| 847 | } |
| 848 | void CatchEndPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) { |
| 849 | assert(Idx == 0); |
| 850 | setUnwindDest(B); |
| 851 | } |
| 852 | |
| 853 | //===----------------------------------------------------------------------===// |
| 854 | // CatchReturnInst Implementation |
| 855 | //===----------------------------------------------------------------------===// |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 856 | void CatchReturnInst::init(CatchPadInst *CatchPad, BasicBlock *BB) { |
| 857 | Op<0>() = CatchPad; |
| 858 | Op<1>() = BB; |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 859 | } |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 860 | |
| 861 | CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI) |
| 862 | : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet, |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 863 | OperandTraits<CatchReturnInst>::op_begin(this), 2) { |
| 864 | Op<0>() = CRI.Op<0>(); |
| 865 | Op<1>() = CRI.Op<1>(); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 868 | CatchReturnInst::CatchReturnInst(CatchPadInst *CatchPad, BasicBlock *BB, |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 869 | Instruction *InsertBefore) |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 870 | : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet, |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 871 | OperandTraits<CatchReturnInst>::op_begin(this), 2, |
| 872 | InsertBefore) { |
| 873 | init(CatchPad, BB); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 876 | CatchReturnInst::CatchReturnInst(CatchPadInst *CatchPad, BasicBlock *BB, |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 877 | BasicBlock *InsertAtEnd) |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 878 | : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet, |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 879 | OperandTraits<CatchReturnInst>::op_begin(this), 2, |
| 880 | InsertAtEnd) { |
| 881 | init(CatchPad, BB); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | BasicBlock *CatchReturnInst::getSuccessorV(unsigned Idx) const { |
David Majnemer | b01aa9f | 2015-08-23 19:22:31 +0000 | [diff] [blame] | 885 | assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!"); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 886 | return getSuccessor(); |
| 887 | } |
| 888 | unsigned CatchReturnInst::getNumSuccessorsV() const { |
| 889 | return getNumSuccessors(); |
| 890 | } |
| 891 | void CatchReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) { |
David Majnemer | b01aa9f | 2015-08-23 19:22:31 +0000 | [diff] [blame] | 892 | assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!"); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 893 | setSuccessor(B); |
| 894 | } |
| 895 | |
| 896 | //===----------------------------------------------------------------------===// |
| 897 | // CatchPadInst Implementation |
| 898 | //===----------------------------------------------------------------------===// |
| 899 | void CatchPadInst::init(BasicBlock *IfNormal, BasicBlock *IfException, |
David Majnemer | 5c73c94 | 2015-08-13 22:11:40 +0000 | [diff] [blame] | 900 | ArrayRef<Value *> Args, const Twine &NameStr) { |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 901 | assert(getNumOperands() == 2 + Args.size() && "NumOperands not set up?"); |
| 902 | Op<-2>() = IfNormal; |
| 903 | Op<-1>() = IfException; |
| 904 | std::copy(Args.begin(), Args.end(), op_begin()); |
| 905 | setName(NameStr); |
| 906 | } |
| 907 | |
| 908 | CatchPadInst::CatchPadInst(const CatchPadInst &CPI) |
| 909 | : TerminatorInst(CPI.getType(), Instruction::CatchPad, |
| 910 | OperandTraits<CatchPadInst>::op_end(this) - |
| 911 | CPI.getNumOperands(), |
| 912 | CPI.getNumOperands()) { |
| 913 | std::copy(CPI.op_begin(), CPI.op_end(), op_begin()); |
| 914 | } |
| 915 | |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 916 | CatchPadInst::CatchPadInst(BasicBlock *IfNormal, BasicBlock *IfException, |
| 917 | ArrayRef<Value *> Args, unsigned Values, |
| 918 | const Twine &NameStr, Instruction *InsertBefore) |
| 919 | : TerminatorInst(Type::getTokenTy(IfNormal->getContext()), |
| 920 | Instruction::CatchPad, |
David Majnemer | 5c73c94 | 2015-08-13 22:11:40 +0000 | [diff] [blame] | 921 | OperandTraits<CatchPadInst>::op_end(this) - Values, Values, |
| 922 | InsertBefore) { |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 923 | init(IfNormal, IfException, Args, NameStr); |
| 924 | } |
| 925 | |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 926 | CatchPadInst::CatchPadInst(BasicBlock *IfNormal, BasicBlock *IfException, |
| 927 | ArrayRef<Value *> Args, unsigned Values, |
| 928 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
| 929 | : TerminatorInst(Type::getTokenTy(IfNormal->getContext()), |
| 930 | Instruction::CatchPad, |
David Majnemer | 5c73c94 | 2015-08-13 22:11:40 +0000 | [diff] [blame] | 931 | OperandTraits<CatchPadInst>::op_end(this) - Values, Values, |
| 932 | InsertAtEnd) { |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 933 | init(IfNormal, IfException, Args, NameStr); |
| 934 | } |
| 935 | |
| 936 | BasicBlock *CatchPadInst::getSuccessorV(unsigned Idx) const { |
| 937 | return getSuccessor(Idx); |
| 938 | } |
| 939 | unsigned CatchPadInst::getNumSuccessorsV() const { |
| 940 | return getNumSuccessors(); |
| 941 | } |
| 942 | void CatchPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) { |
| 943 | return setSuccessor(Idx, B); |
| 944 | } |
| 945 | |
| 946 | //===----------------------------------------------------------------------===// |
| 947 | // TerminatePadInst Implementation |
| 948 | //===----------------------------------------------------------------------===// |
David Majnemer | 2a2b242 | 2015-08-06 21:08:36 +0000 | [diff] [blame] | 949 | void TerminatePadInst::init(BasicBlock *BB, ArrayRef<Value *> Args) { |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 950 | if (BB) |
| 951 | setInstructionSubclassData(getSubclassDataFromInstruction() | 1); |
| 952 | if (BB) |
| 953 | Op<-1>() = BB; |
| 954 | std::copy(Args.begin(), Args.end(), op_begin()); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | TerminatePadInst::TerminatePadInst(const TerminatePadInst &TPI) |
| 958 | : TerminatorInst(TPI.getType(), Instruction::TerminatePad, |
| 959 | OperandTraits<TerminatePadInst>::op_end(this) - |
| 960 | TPI.getNumOperands(), |
| 961 | TPI.getNumOperands()) { |
David Majnemer | eb518bd | 2015-08-04 08:21:40 +0000 | [diff] [blame] | 962 | setInstructionSubclassData(TPI.getSubclassDataFromInstruction()); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 963 | std::copy(TPI.op_begin(), TPI.op_end(), op_begin()); |
| 964 | } |
| 965 | |
| 966 | TerminatePadInst::TerminatePadInst(LLVMContext &C, BasicBlock *BB, |
David Majnemer | 2a2b242 | 2015-08-06 21:08:36 +0000 | [diff] [blame] | 967 | ArrayRef<Value *> Args, unsigned Values, |
| 968 | Instruction *InsertBefore) |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 969 | : TerminatorInst(Type::getVoidTy(C), Instruction::TerminatePad, |
| 970 | OperandTraits<TerminatePadInst>::op_end(this) - Values, |
| 971 | Values, InsertBefore) { |
David Majnemer | 2a2b242 | 2015-08-06 21:08:36 +0000 | [diff] [blame] | 972 | init(BB, Args); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | TerminatePadInst::TerminatePadInst(LLVMContext &C, BasicBlock *BB, |
David Majnemer | 2a2b242 | 2015-08-06 21:08:36 +0000 | [diff] [blame] | 976 | ArrayRef<Value *> Args, unsigned Values, |
| 977 | BasicBlock *InsertAtEnd) |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 978 | : TerminatorInst(Type::getVoidTy(C), Instruction::TerminatePad, |
| 979 | OperandTraits<TerminatePadInst>::op_end(this) - Values, |
| 980 | Values, InsertAtEnd) { |
David Majnemer | 2a2b242 | 2015-08-06 21:08:36 +0000 | [diff] [blame] | 981 | init(BB, Args); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | BasicBlock *TerminatePadInst::getSuccessorV(unsigned Idx) const { |
| 985 | assert(Idx == 0); |
| 986 | return getUnwindDest(); |
| 987 | } |
| 988 | unsigned TerminatePadInst::getNumSuccessorsV() const { |
| 989 | return getNumSuccessors(); |
| 990 | } |
| 991 | void TerminatePadInst::setSuccessorV(unsigned Idx, BasicBlock *B) { |
| 992 | assert(Idx == 0); |
| 993 | return setUnwindDest(B); |
| 994 | } |
| 995 | |
| 996 | //===----------------------------------------------------------------------===// |
| 997 | // CleanupPadInst Implementation |
| 998 | //===----------------------------------------------------------------------===// |
| 999 | void CleanupPadInst::init(ArrayRef<Value *> Args, const Twine &NameStr) { |
| 1000 | assert(getNumOperands() == Args.size() && "NumOperands not set up?"); |
| 1001 | std::copy(Args.begin(), Args.end(), op_begin()); |
| 1002 | setName(NameStr); |
| 1003 | } |
| 1004 | |
| 1005 | CleanupPadInst::CleanupPadInst(const CleanupPadInst &CPI) |
| 1006 | : Instruction(CPI.getType(), Instruction::CleanupPad, |
| 1007 | OperandTraits<CleanupPadInst>::op_end(this) - |
| 1008 | CPI.getNumOperands(), |
| 1009 | CPI.getNumOperands()) { |
| 1010 | std::copy(CPI.op_begin(), CPI.op_end(), op_begin()); |
| 1011 | } |
| 1012 | |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 1013 | CleanupPadInst::CleanupPadInst(LLVMContext &C, ArrayRef<Value *> Args, |
David Majnemer | 5c73c94 | 2015-08-13 22:11:40 +0000 | [diff] [blame] | 1014 | const Twine &NameStr, Instruction *InsertBefore) |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 1015 | : Instruction(Type::getTokenTy(C), Instruction::CleanupPad, |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 1016 | OperandTraits<CleanupPadInst>::op_end(this) - Args.size(), |
| 1017 | Args.size(), InsertBefore) { |
| 1018 | init(Args, NameStr); |
| 1019 | } |
| 1020 | |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 1021 | CleanupPadInst::CleanupPadInst(LLVMContext &C, ArrayRef<Value *> Args, |
David Majnemer | 5c73c94 | 2015-08-13 22:11:40 +0000 | [diff] [blame] | 1022 | const Twine &NameStr, BasicBlock *InsertAtEnd) |
Joseph Tremoulet | 8220bcc | 2015-08-23 00:26:33 +0000 | [diff] [blame] | 1023 | : Instruction(Type::getTokenTy(C), Instruction::CleanupPad, |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 1024 | OperandTraits<CleanupPadInst>::op_end(this) - Args.size(), |
| 1025 | Args.size(), InsertAtEnd) { |
| 1026 | init(Args, NameStr); |
| 1027 | } |
| 1028 | |
| 1029 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 1030 | // UnreachableInst Implementation |
| 1031 | //===----------------------------------------------------------------------===// |
| 1032 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1033 | UnreachableInst::UnreachableInst(LLVMContext &Context, |
| 1034 | Instruction *InsertBefore) |
| 1035 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable, |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1036 | nullptr, 0, InsertBefore) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1037 | } |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1038 | UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
| 1039 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable, |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1040 | nullptr, 0, InsertAtEnd) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1043 | unsigned UnreachableInst::getNumSuccessorsV() const { |
| 1044 | return getNumSuccessors(); |
| 1045 | } |
| 1046 | |
| 1047 | void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Bill Wendling | 0aef16a | 2012-02-06 21:44:22 +0000 | [diff] [blame] | 1048 | llvm_unreachable("UnreachableInst has no successors!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const { |
Bill Wendling | 0aef16a | 2012-02-06 21:44:22 +0000 | [diff] [blame] | 1052 | llvm_unreachable("UnreachableInst has no successors!"); |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1056 | // BranchInst Implementation |
| 1057 | //===----------------------------------------------------------------------===// |
| 1058 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1059 | void BranchInst::AssertOK() { |
| 1060 | if (isConditional()) |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1061 | assert(getCondition()->getType()->isIntegerTy(1) && |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1062 | "May only branch on boolean predicates!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1065 | BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1066 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1067 | OperandTraits<BranchInst>::op_end(this) - 1, |
| 1068 | 1, InsertBefore) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1069 | assert(IfTrue && "Branch destination may not be null!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1070 | Op<-1>() = IfTrue; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1071 | } |
| 1072 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 1073 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1074 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1075 | OperandTraits<BranchInst>::op_end(this) - 3, |
| 1076 | 3, InsertBefore) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1077 | Op<-1>() = IfTrue; |
| 1078 | Op<-2>() = IfFalse; |
| 1079 | Op<-3>() = Cond; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1080 | #ifndef NDEBUG |
| 1081 | AssertOK(); |
| 1082 | #endif |
| 1083 | } |
| 1084 | |
| 1085 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1086 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1087 | OperandTraits<BranchInst>::op_end(this) - 1, |
| 1088 | 1, InsertAtEnd) { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 1089 | assert(IfTrue && "Branch destination may not be null!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1090 | Op<-1>() = IfTrue; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 1094 | BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1095 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1096 | OperandTraits<BranchInst>::op_end(this) - 3, |
| 1097 | 3, InsertAtEnd) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1098 | Op<-1>() = IfTrue; |
| 1099 | Op<-2>() = IfFalse; |
| 1100 | Op<-3>() = Cond; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1101 | #ifndef NDEBUG |
| 1102 | AssertOK(); |
| 1103 | #endif |
| 1104 | } |
| 1105 | |
| 1106 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1107 | BranchInst::BranchInst(const BranchInst &BI) : |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1108 | TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1109 | OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(), |
| 1110 | BI.getNumOperands()) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1111 | Op<-1>() = BI.Op<-1>(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1112 | if (BI.getNumOperands() != 1) { |
| 1113 | assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 1114 | Op<-3>() = BI.Op<-3>(); |
| 1115 | Op<-2>() = BI.Op<-2>(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1116 | } |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1117 | SubclassOptionalData = BI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Chandler Carruth | 3e8aa65 | 2011-10-17 01:11:57 +0000 | [diff] [blame] | 1120 | void BranchInst::swapSuccessors() { |
| 1121 | assert(isConditional() && |
| 1122 | "Cannot swap successors of an unconditional branch"); |
| 1123 | Op<-1>().swap(Op<-2>()); |
| 1124 | |
| 1125 | // Update profile metadata if present and it matches our structural |
| 1126 | // expectations. |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1127 | MDNode *ProfileData = getMetadata(LLVMContext::MD_prof); |
Chandler Carruth | 3e8aa65 | 2011-10-17 01:11:57 +0000 | [diff] [blame] | 1128 | if (!ProfileData || ProfileData->getNumOperands() != 3) |
| 1129 | return; |
| 1130 | |
| 1131 | // The first operand is the name. Fetch them backwards and build a new one. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1132 | Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2), |
| 1133 | ProfileData->getOperand(1)}; |
Chandler Carruth | 3e8aa65 | 2011-10-17 01:11:57 +0000 | [diff] [blame] | 1134 | setMetadata(LLVMContext::MD_prof, |
| 1135 | MDNode::get(ProfileData->getContext(), Ops)); |
| 1136 | } |
| 1137 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1138 | BasicBlock *BranchInst::getSuccessorV(unsigned idx) const { |
| 1139 | return getSuccessor(idx); |
| 1140 | } |
| 1141 | unsigned BranchInst::getNumSuccessorsV() const { |
| 1142 | return getNumSuccessors(); |
| 1143 | } |
| 1144 | void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 1145 | setSuccessor(idx, B); |
| 1146 | } |
| 1147 | |
| 1148 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1149 | //===----------------------------------------------------------------------===// |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1150 | // AllocaInst Implementation |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1151 | //===----------------------------------------------------------------------===// |
| 1152 | |
Owen Anderson | b6b2530 | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 1153 | static Value *getAISize(LLVMContext &Context, Value *Amt) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1154 | if (!Amt) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1155 | Amt = ConstantInt::get(Type::getInt32Ty(Context), 1); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 1156 | else { |
| 1157 | assert(!isa<BasicBlock>(Amt) && |
Chris Lattner | 9b6ec77 | 2007-10-18 16:10:48 +0000 | [diff] [blame] | 1158 | "Passed basic block into allocation size parameter! Use other ctor"); |
Dan Gohman | 2140a74 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 1159 | assert(Amt->getType()->isIntegerTy() && |
| 1160 | "Allocation array size is not an integer!"); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 1161 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1162 | return Amt; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1165 | AllocaInst::AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore) |
| 1166 | : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertBefore) {} |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1167 | |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1168 | AllocaInst::AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd) |
| 1169 | : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertAtEnd) {} |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1170 | |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1171 | AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1172 | Instruction *InsertBefore) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1173 | : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertBefore) {} |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1174 | |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1175 | AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1176 | BasicBlock *InsertAtEnd) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1177 | : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertAtEnd) {} |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1178 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1179 | AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1180 | const Twine &Name, Instruction *InsertBefore) |
David Blaikie | bf0a42a | 2015-04-29 23:00:35 +0000 | [diff] [blame] | 1181 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
| 1182 | getAISize(Ty->getContext(), ArraySize), InsertBefore), |
| 1183 | AllocatedType(Ty) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1184 | setAlignment(Align); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1185 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1186 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1189 | AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align, |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1190 | const Twine &Name, BasicBlock *InsertAtEnd) |
David Blaikie | bf0a42a | 2015-04-29 23:00:35 +0000 | [diff] [blame] | 1191 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
| 1192 | getAISize(Ty->getContext(), ArraySize), InsertAtEnd), |
| 1193 | AllocatedType(Ty) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1194 | setAlignment(Align); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 1195 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1196 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 1199 | // Out of line virtual method, so the vtable, etc has a home. |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1200 | AllocaInst::~AllocaInst() { |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1203 | void AllocaInst::setAlignment(unsigned Align) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1204 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1205 | assert(Align <= MaximumAlignment && |
| 1206 | "Alignment is greater than MaximumAlignment!"); |
Reid Kleckner | 436c42e | 2014-01-17 23:58:17 +0000 | [diff] [blame] | 1207 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) | |
| 1208 | (Log2_32(Align) + 1)); |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 1209 | assert(getAlignment() == Align && "Alignment representation error!"); |
| 1210 | } |
| 1211 | |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1212 | bool AllocaInst::isArrayAllocation() const { |
Reid Spencer | a9e6e31 | 2007-03-01 20:27:41 +0000 | [diff] [blame] | 1213 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0))) |
Dan Gohman | 9a1b859 | 2010-09-27 15:15:44 +0000 | [diff] [blame] | 1214 | return !CI->isOne(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1215 | return true; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Chris Lattner | 8b291e6 | 2008-11-26 02:54:17 +0000 | [diff] [blame] | 1218 | /// isStaticAlloca - Return true if this alloca is in the entry block of the |
| 1219 | /// function and is a constant size. If so, the code generator will fold it |
| 1220 | /// into the prolog/epilog code, so it is basically free. |
| 1221 | bool AllocaInst::isStaticAlloca() const { |
| 1222 | // Must be constant size. |
| 1223 | if (!isa<ConstantInt>(getArraySize())) return false; |
| 1224 | |
| 1225 | // Must be in the entry block. |
| 1226 | const BasicBlock *Parent = getParent(); |
Reid Kleckner | 436c42e | 2014-01-17 23:58:17 +0000 | [diff] [blame] | 1227 | return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca(); |
Chris Lattner | 8b291e6 | 2008-11-26 02:54:17 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1230 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1231 | // LoadInst Implementation |
| 1232 | //===----------------------------------------------------------------------===// |
| 1233 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1234 | void LoadInst::AssertOK() { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1235 | assert(getOperand(0)->getType()->isPointerTy() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1236 | "Ptr must have pointer type."); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1237 | assert(!(isAtomic() && getAlignment() == 0) && |
| 1238 | "Alignment required for atomic load"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1241 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1242 | : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {} |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1243 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1244 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1245 | : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {} |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1246 | |
David Blaikie | 0c28fd7 | 2015-05-20 21:46:30 +0000 | [diff] [blame] | 1247 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1248 | Instruction *InsertBef) |
David Blaikie | 0c28fd7 | 2015-05-20 21:46:30 +0000 | [diff] [blame] | 1249 | : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {} |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1250 | |
| 1251 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
| 1252 | BasicBlock *InsertAE) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1253 | : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {} |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1254 | |
David Blaikie | b7a02987 | 2015-04-17 19:56:21 +0000 | [diff] [blame] | 1255 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1256 | unsigned Align, Instruction *InsertBef) |
David Blaikie | b7a02987 | 2015-04-17 19:56:21 +0000 | [diff] [blame] | 1257 | : LoadInst(Ty, Ptr, Name, isVolatile, Align, NotAtomic, CrossThread, |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1258 | InsertBef) {} |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1259 | |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1260 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1261 | unsigned Align, BasicBlock *InsertAE) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1262 | : LoadInst(Ptr, Name, isVolatile, Align, NotAtomic, CrossThread, InsertAE) { |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
David Blaikie | 15d9a4c | 2015-04-06 20:59:48 +0000 | [diff] [blame] | 1265 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1266 | unsigned Align, AtomicOrdering Order, |
David Blaikie | 15d9a4c | 2015-04-06 20:59:48 +0000 | [diff] [blame] | 1267 | SynchronizationScope SynchScope, Instruction *InsertBef) |
| 1268 | : UnaryInstruction(Ty, Load, Ptr, InsertBef) { |
David Blaikie | 79009f8 | 2015-05-20 20:22:31 +0000 | [diff] [blame] | 1269 | assert(Ty == cast<PointerType>(Ptr->getType())->getElementType()); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1270 | setVolatile(isVolatile); |
| 1271 | setAlignment(Align); |
| 1272 | setAtomic(Order, SynchScope); |
| 1273 | AssertOK(); |
| 1274 | setName(Name); |
| 1275 | } |
| 1276 | |
| 1277 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
| 1278 | unsigned Align, AtomicOrdering Order, |
| 1279 | SynchronizationScope SynchScope, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1280 | BasicBlock *InsertAE) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1281 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1282 | Load, Ptr, InsertAE) { |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1283 | setVolatile(isVolatile); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1284 | setAlignment(Align); |
| 1285 | setAtomic(Order, SynchScope); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 1286 | AssertOK(); |
| 1287 | setName(Name); |
| 1288 | } |
| 1289 | |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1290 | LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef) |
| 1291 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1292 | Load, Ptr, InsertBef) { |
| 1293 | setVolatile(false); |
| 1294 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1295 | setAtomic(NotAtomic); |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1296 | AssertOK(); |
| 1297 | if (Name && Name[0]) setName(Name); |
| 1298 | } |
| 1299 | |
| 1300 | LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE) |
| 1301 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1302 | Load, Ptr, InsertAE) { |
| 1303 | setVolatile(false); |
| 1304 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1305 | setAtomic(NotAtomic); |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1306 | AssertOK(); |
| 1307 | if (Name && Name[0]) setName(Name); |
| 1308 | } |
| 1309 | |
David Blaikie | 0c28fd7 | 2015-05-20 21:46:30 +0000 | [diff] [blame] | 1310 | LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile, |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1311 | Instruction *InsertBef) |
David Blaikie | 0c28fd7 | 2015-05-20 21:46:30 +0000 | [diff] [blame] | 1312 | : UnaryInstruction(Ty, Load, Ptr, InsertBef) { |
| 1313 | assert(Ty == cast<PointerType>(Ptr->getType())->getElementType()); |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1314 | setVolatile(isVolatile); |
| 1315 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1316 | setAtomic(NotAtomic); |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1317 | AssertOK(); |
| 1318 | if (Name && Name[0]) setName(Name); |
| 1319 | } |
| 1320 | |
| 1321 | LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, |
| 1322 | BasicBlock *InsertAE) |
| 1323 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1324 | Load, Ptr, InsertAE) { |
| 1325 | setVolatile(isVolatile); |
| 1326 | setAlignment(0); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1327 | setAtomic(NotAtomic); |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 1328 | AssertOK(); |
| 1329 | if (Name && Name[0]) setName(Name); |
| 1330 | } |
| 1331 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1332 | void LoadInst::setAlignment(unsigned Align) { |
| 1333 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1334 | assert(Align <= MaximumAlignment && |
| 1335 | "Alignment is greater than MaximumAlignment!"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1336 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) | |
Chris Lattner | d8eb2cf | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 1337 | ((Log2_32(Align)+1)<<1)); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1338 | assert(getAlignment() == Align && "Alignment representation error!"); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1339 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1340 | |
| 1341 | //===----------------------------------------------------------------------===// |
| 1342 | // StoreInst Implementation |
| 1343 | //===----------------------------------------------------------------------===// |
| 1344 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1345 | void StoreInst::AssertOK() { |
Nate Begeman | fecbc8c | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 1346 | assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1347 | assert(getOperand(1)->getType()->isPointerTy() && |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1348 | "Ptr must have pointer type!"); |
| 1349 | assert(getOperand(0)->getType() == |
| 1350 | cast<PointerType>(getOperand(1)->getType())->getElementType() |
Alkis Evlogimenos | 079fbde | 2004-08-06 14:33:37 +0000 | [diff] [blame] | 1351 | && "Ptr must be a pointer to Val type!"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1352 | assert(!(isAtomic() && getAlignment() == 0) && |
Mark Lacey | 1d7c97e | 2013-12-21 00:00:49 +0000 | [diff] [blame] | 1353 | "Alignment required for atomic store"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1354 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1355 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1356 | StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1357 | : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {} |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1358 | |
| 1359 | StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1360 | : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {} |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1361 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1362 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1363 | Instruction *InsertBefore) |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1364 | : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {} |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1365 | |
| 1366 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Benjamin Kramer | fc165f1 | 2015-03-05 22:05:26 +0000 | [diff] [blame] | 1367 | BasicBlock *InsertAtEnd) |
| 1368 | : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {} |
| 1369 | |
| 1370 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align, |
| 1371 | Instruction *InsertBefore) |
| 1372 | : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread, |
| 1373 | InsertBefore) {} |
| 1374 | |
| 1375 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align, |
| 1376 | BasicBlock *InsertAtEnd) |
| 1377 | : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread, |
| 1378 | InsertAtEnd) {} |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1379 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1380 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1381 | unsigned Align, AtomicOrdering Order, |
| 1382 | SynchronizationScope SynchScope, |
| 1383 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1384 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1385 | OperandTraits<StoreInst>::op_begin(this), |
| 1386 | OperandTraits<StoreInst>::operands(this), |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1387 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1388 | Op<0>() = val; |
| 1389 | Op<1>() = addr; |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1390 | setVolatile(isVolatile); |
| 1391 | setAlignment(Align); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1392 | setAtomic(Order, SynchScope); |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1393 | AssertOK(); |
| 1394 | } |
| 1395 | |
| 1396 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1397 | unsigned Align, AtomicOrdering Order, |
| 1398 | SynchronizationScope SynchScope, |
| 1399 | BasicBlock *InsertAtEnd) |
| 1400 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
| 1401 | OperandTraits<StoreInst>::op_begin(this), |
| 1402 | OperandTraits<StoreInst>::operands(this), |
| 1403 | InsertAtEnd) { |
| 1404 | Op<0>() = val; |
| 1405 | Op<1>() = addr; |
| 1406 | setVolatile(isVolatile); |
| 1407 | setAlignment(Align); |
| 1408 | setAtomic(Order, SynchScope); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1409 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1412 | void StoreInst::setAlignment(unsigned Align) { |
| 1413 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1414 | assert(Align <= MaximumAlignment && |
| 1415 | "Alignment is greater than MaximumAlignment!"); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1416 | setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) | |
Chris Lattner | d8eb2cf | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 1417 | ((Log2_32(Align)+1) << 1)); |
Dan Gohman | a7e5a24 | 2010-07-28 20:12:04 +0000 | [diff] [blame] | 1418 | assert(getAlignment() == Align && "Alignment representation error!"); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1421 | //===----------------------------------------------------------------------===// |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1422 | // AtomicCmpXchgInst Implementation |
| 1423 | //===----------------------------------------------------------------------===// |
| 1424 | |
| 1425 | void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal, |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1426 | AtomicOrdering SuccessOrdering, |
| 1427 | AtomicOrdering FailureOrdering, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1428 | SynchronizationScope SynchScope) { |
| 1429 | Op<0>() = Ptr; |
| 1430 | Op<1>() = Cmp; |
| 1431 | Op<2>() = NewVal; |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1432 | setSuccessOrdering(SuccessOrdering); |
| 1433 | setFailureOrdering(FailureOrdering); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1434 | setSynchScope(SynchScope); |
| 1435 | |
| 1436 | assert(getOperand(0) && getOperand(1) && getOperand(2) && |
| 1437 | "All operands must be non-null!"); |
| 1438 | assert(getOperand(0)->getType()->isPointerTy() && |
| 1439 | "Ptr must have pointer type!"); |
| 1440 | assert(getOperand(1)->getType() == |
| 1441 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1442 | && "Ptr must be a pointer to Cmp type!"); |
| 1443 | assert(getOperand(2)->getType() == |
| 1444 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1445 | && "Ptr must be a pointer to NewVal type!"); |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1446 | assert(SuccessOrdering != NotAtomic && |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1447 | "AtomicCmpXchg instructions must be atomic!"); |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1448 | assert(FailureOrdering != NotAtomic && |
| 1449 | "AtomicCmpXchg instructions must be atomic!"); |
| 1450 | assert(SuccessOrdering >= FailureOrdering && |
| 1451 | "AtomicCmpXchg success ordering must be at least as strong as fail"); |
| 1452 | assert(FailureOrdering != Release && FailureOrdering != AcquireRelease && |
| 1453 | "AtomicCmpXchg failure ordering cannot include release semantics"); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1457 | AtomicOrdering SuccessOrdering, |
| 1458 | AtomicOrdering FailureOrdering, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1459 | SynchronizationScope SynchScope, |
| 1460 | Instruction *InsertBefore) |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1461 | : Instruction( |
| 1462 | StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()), |
| 1463 | nullptr), |
| 1464 | AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this), |
| 1465 | OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) { |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1466 | Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1470 | AtomicOrdering SuccessOrdering, |
| 1471 | AtomicOrdering FailureOrdering, |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1472 | SynchronizationScope SynchScope, |
| 1473 | BasicBlock *InsertAtEnd) |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1474 | : Instruction( |
| 1475 | StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()), |
| 1476 | nullptr), |
| 1477 | AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this), |
| 1478 | OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) { |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 1479 | Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1480 | } |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 1481 | |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1482 | //===----------------------------------------------------------------------===// |
| 1483 | // AtomicRMWInst Implementation |
| 1484 | //===----------------------------------------------------------------------===// |
| 1485 | |
| 1486 | void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val, |
| 1487 | AtomicOrdering Ordering, |
| 1488 | SynchronizationScope SynchScope) { |
| 1489 | Op<0>() = Ptr; |
| 1490 | Op<1>() = Val; |
| 1491 | setOperation(Operation); |
| 1492 | setOrdering(Ordering); |
| 1493 | setSynchScope(SynchScope); |
| 1494 | |
| 1495 | assert(getOperand(0) && getOperand(1) && |
| 1496 | "All operands must be non-null!"); |
| 1497 | assert(getOperand(0)->getType()->isPointerTy() && |
| 1498 | "Ptr must have pointer type!"); |
| 1499 | assert(getOperand(1)->getType() == |
| 1500 | cast<PointerType>(getOperand(0)->getType())->getElementType() |
| 1501 | && "Ptr must be a pointer to Val type!"); |
| 1502 | assert(Ordering != NotAtomic && |
| 1503 | "AtomicRMW instructions must be atomic!"); |
| 1504 | } |
| 1505 | |
| 1506 | AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, |
| 1507 | AtomicOrdering Ordering, |
| 1508 | SynchronizationScope SynchScope, |
| 1509 | Instruction *InsertBefore) |
| 1510 | : Instruction(Val->getType(), AtomicRMW, |
| 1511 | OperandTraits<AtomicRMWInst>::op_begin(this), |
| 1512 | OperandTraits<AtomicRMWInst>::operands(this), |
| 1513 | InsertBefore) { |
| 1514 | Init(Operation, Ptr, Val, Ordering, SynchScope); |
| 1515 | } |
| 1516 | |
| 1517 | AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, |
| 1518 | AtomicOrdering Ordering, |
| 1519 | SynchronizationScope SynchScope, |
| 1520 | BasicBlock *InsertAtEnd) |
| 1521 | : Instruction(Val->getType(), AtomicRMW, |
| 1522 | OperandTraits<AtomicRMWInst>::op_begin(this), |
| 1523 | OperandTraits<AtomicRMWInst>::operands(this), |
| 1524 | InsertAtEnd) { |
| 1525 | Init(Operation, Ptr, Val, Ordering, SynchScope); |
| 1526 | } |
| 1527 | |
| 1528 | //===----------------------------------------------------------------------===// |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1529 | // FenceInst Implementation |
| 1530 | //===----------------------------------------------------------------------===// |
| 1531 | |
| 1532 | FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, |
| 1533 | SynchronizationScope SynchScope, |
| 1534 | Instruction *InsertBefore) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1535 | : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) { |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1536 | setOrdering(Ordering); |
| 1537 | setSynchScope(SynchScope); |
| 1538 | } |
| 1539 | |
| 1540 | FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering, |
| 1541 | SynchronizationScope SynchScope, |
| 1542 | BasicBlock *InsertAtEnd) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1543 | : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) { |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1544 | setOrdering(Ordering); |
| 1545 | setSynchScope(SynchScope); |
| 1546 | } |
| 1547 | |
| 1548 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1549 | // GetElementPtrInst Implementation |
| 1550 | //===----------------------------------------------------------------------===// |
| 1551 | |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1552 | void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1553 | const Twine &Name) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 1554 | assert(getNumOperands() == 1 + IdxList.size() && |
| 1555 | "NumOperands not initialized?"); |
Pete Cooper | eb31b68 | 2015-05-21 22:48:54 +0000 | [diff] [blame] | 1556 | Op<0>() = Ptr; |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1557 | std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1); |
Matthijs Kooijman | 76d8dec | 2008-06-04 16:14:12 +0000 | [diff] [blame] | 1558 | setName(Name); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1561 | GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI) |
David Blaikie | 73cf872 | 2015-05-05 18:03:48 +0000 | [diff] [blame] | 1562 | : Instruction(GEPI.getType(), GetElementPtr, |
| 1563 | OperandTraits<GetElementPtrInst>::op_end(this) - |
| 1564 | GEPI.getNumOperands(), |
| 1565 | GEPI.getNumOperands()), |
David Blaikie | f5147ef | 2015-06-01 03:09:34 +0000 | [diff] [blame] | 1566 | SourceElementType(GEPI.SourceElementType), |
| 1567 | ResultElementType(GEPI.ResultElementType) { |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1568 | std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin()); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1569 | SubclassOptionalData = GEPI.SubclassOptionalData; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1572 | /// getIndexedType - Returns the type of the element that would be accessed with |
| 1573 | /// a gep instruction with the specified parameters. |
| 1574 | /// |
| 1575 | /// The Idxs pointer should point to a continuous piece of memory containing the |
| 1576 | /// indices, either as Value* or uint64_t. |
| 1577 | /// |
| 1578 | /// A null type is returned if the indices are invalid for the specified |
| 1579 | /// pointer type. |
| 1580 | /// |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1581 | template <typename IndexTy> |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1582 | static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) { |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1583 | // Handle the special case of the empty set index set, which is always valid. |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1584 | if (IdxList.empty()) |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1585 | return Agg; |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1586 | |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1587 | // If there is at least one index, the top level type must be sized, otherwise |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1588 | // it cannot be 'stepped over'. |
| 1589 | if (!Agg->isSized()) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1590 | return nullptr; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1591 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1592 | unsigned CurIdx = 1; |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1593 | for (; CurIdx != IdxList.size(); ++CurIdx) { |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1594 | CompositeType *CT = dyn_cast<CompositeType>(Agg); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1595 | if (!CT || CT->isPointerTy()) return nullptr; |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1596 | IndexTy Index = IdxList[CurIdx]; |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1597 | if (!CT->indexValid(Index)) return nullptr; |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1598 | Agg = CT->getTypeAtIndex(Index); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1599 | } |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1600 | return CurIdx == IdxList.size() ? Agg : nullptr; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1603 | Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) { |
| 1604 | return getIndexedTypeInternal(Ty, IdxList); |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1607 | Type *GetElementPtrInst::getIndexedType(Type *Ty, |
Jay Foad | d1b7849 | 2011-07-25 09:48:08 +0000 | [diff] [blame] | 1608 | ArrayRef<Constant *> IdxList) { |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1609 | return getIndexedTypeInternal(Ty, IdxList); |
Jay Foad | 1d4a8fe | 2011-01-14 08:07:43 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
David Blaikie | d288fb8 | 2015-03-30 21:41:43 +0000 | [diff] [blame] | 1612 | Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) { |
| 1613 | return getIndexedTypeInternal(Ty, IdxList); |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1616 | /// hasAllZeroIndices - Return true if all of the indices of this GEP are |
| 1617 | /// zeros. If so, the result pointer and the first operand have the same |
| 1618 | /// value, just potentially different types. |
| 1619 | bool GetElementPtrInst::hasAllZeroIndices() const { |
| 1620 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1621 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) { |
| 1622 | if (!CI->isZero()) return false; |
| 1623 | } else { |
| 1624 | return false; |
| 1625 | } |
| 1626 | } |
| 1627 | return true; |
| 1628 | } |
| 1629 | |
Chris Lattner | 2705829 | 2007-04-27 20:35:56 +0000 | [diff] [blame] | 1630 | /// hasAllConstantIndices - Return true if all of the indices of this GEP are |
| 1631 | /// constant integers. If so, the result pointer and the first operand have |
| 1632 | /// a constant offset between them. |
| 1633 | bool GetElementPtrInst::hasAllConstantIndices() const { |
| 1634 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1635 | if (!isa<ConstantInt>(getOperand(i))) |
| 1636 | return false; |
| 1637 | } |
| 1638 | return true; |
| 1639 | } |
| 1640 | |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1641 | void GetElementPtrInst::setIsInBounds(bool B) { |
| 1642 | cast<GEPOperator>(this)->setIsInBounds(B); |
| 1643 | } |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1644 | |
Nick Lewycky | 28a5f25 | 2009-09-27 21:33:04 +0000 | [diff] [blame] | 1645 | bool GetElementPtrInst::isInBounds() const { |
| 1646 | return cast<GEPOperator>(this)->isInBounds(); |
| 1647 | } |
| 1648 | |
Chandler Carruth | 1e14053 | 2012-12-11 10:29:10 +0000 | [diff] [blame] | 1649 | bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL, |
| 1650 | APInt &Offset) const { |
Chandler Carruth | 7ec41c7 | 2012-12-11 11:05:15 +0000 | [diff] [blame] | 1651 | // Delegate to the generic GEPOperator implementation. |
| 1652 | return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset); |
Chandler Carruth | 1e14053 | 2012-12-11 10:29:10 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1655 | //===----------------------------------------------------------------------===// |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1656 | // ExtractElementInst Implementation |
| 1657 | //===----------------------------------------------------------------------===// |
| 1658 | |
| 1659 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1660 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1661 | Instruction *InsertBef) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1662 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1663 | ExtractElement, |
| 1664 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1665 | 2, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1666 | assert(isValidOperands(Val, Index) && |
| 1667 | "Invalid extractelement instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1668 | Op<0>() = Val; |
| 1669 | Op<1>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1670 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
| 1673 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1674 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1675 | BasicBlock *InsertAE) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1676 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1677 | ExtractElement, |
| 1678 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1679 | 2, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1680 | assert(isValidOperands(Val, Index) && |
| 1681 | "Invalid extractelement instruction operands!"); |
| 1682 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1683 | Op<0>() = Val; |
| 1684 | Op<1>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1685 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1688 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1689 | bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 1690 | if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy()) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1691 | return false; |
| 1692 | return true; |
| 1693 | } |
| 1694 | |
| 1695 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1696 | //===----------------------------------------------------------------------===// |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1697 | // InsertElementInst Implementation |
| 1698 | //===----------------------------------------------------------------------===// |
| 1699 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1700 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1701 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1702 | Instruction *InsertBef) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1703 | : Instruction(Vec->getType(), InsertElement, |
| 1704 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1705 | 3, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1706 | assert(isValidOperands(Vec, Elt, Index) && |
| 1707 | "Invalid insertelement instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1708 | Op<0>() = Vec; |
| 1709 | Op<1>() = Elt; |
| 1710 | Op<2>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1711 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1714 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1715 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1716 | BasicBlock *InsertAE) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1717 | : Instruction(Vec->getType(), InsertElement, |
| 1718 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1719 | 3, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1720 | assert(isValidOperands(Vec, Elt, Index) && |
| 1721 | "Invalid insertelement instruction operands!"); |
| 1722 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1723 | Op<0>() = Vec; |
| 1724 | Op<1>() = Elt; |
| 1725 | Op<2>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1726 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1729 | bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, |
| 1730 | const Value *Index) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1731 | if (!Vec->getType()->isVectorTy()) |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1732 | return false; // First operand of insertelement must be vector type. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1733 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1734 | if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType()) |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1735 | return false;// Second operand of insertelement must be vector element type. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1736 | |
Michael J. Spencer | 1f10c5ea | 2014-05-01 22:12:39 +0000 | [diff] [blame] | 1737 | if (!Index->getType()->isIntegerTy()) |
Dan Gohman | 4fe64de | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 1738 | return false; // Third operand of insertelement must be i32. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1739 | return true; |
| 1740 | } |
| 1741 | |
| 1742 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1743 | //===----------------------------------------------------------------------===// |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1744 | // ShuffleVectorInst Implementation |
| 1745 | //===----------------------------------------------------------------------===// |
| 1746 | |
| 1747 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1748 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1749 | Instruction *InsertBefore) |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1750 | : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1751 | cast<VectorType>(Mask->getType())->getNumElements()), |
| 1752 | ShuffleVector, |
| 1753 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1754 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1755 | InsertBefore) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1756 | assert(isValidOperands(V1, V2, Mask) && |
| 1757 | "Invalid shuffle vector instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1758 | Op<0>() = V1; |
| 1759 | Op<1>() = V2; |
| 1760 | Op<2>() = Mask; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1761 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1765 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1766 | BasicBlock *InsertAtEnd) |
Dan Gohman | e5af8cd | 2009-08-25 23:27:45 +0000 | [diff] [blame] | 1767 | : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), |
| 1768 | cast<VectorType>(Mask->getType())->getNumElements()), |
| 1769 | ShuffleVector, |
| 1770 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1771 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1772 | InsertAtEnd) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1773 | assert(isValidOperands(V1, V2, Mask) && |
| 1774 | "Invalid shuffle vector instruction operands!"); |
| 1775 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1776 | Op<0>() = V1; |
| 1777 | Op<1>() = V2; |
| 1778 | Op<2>() = Mask; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1779 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1782 | bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1783 | const Value *Mask) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1784 | // V1 and V2 must be vectors of the same type. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1785 | if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType()) |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1786 | return false; |
| 1787 | |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1788 | // Mask must be vector of i32. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1789 | VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType()); |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1790 | if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32)) |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1791 | return false; |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1792 | |
| 1793 | // Check to see if Mask is valid. |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1794 | if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask)) |
| 1795 | return true; |
| 1796 | |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1797 | if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1798 | unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements(); |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 1799 | for (Value *Op : MV->operands()) { |
| 1800 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1801 | if (CI->uge(V1Size*2)) |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1802 | return false; |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 1803 | } else if (!isa<UndefValue>(Op)) { |
Nate Begeman | 60a31c3 | 2010-08-13 00:16:46 +0000 | [diff] [blame] | 1804 | return false; |
| 1805 | } |
| 1806 | } |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1807 | return true; |
Mon P Wang | 6ebf401 | 2011-10-26 00:34:48 +0000 | [diff] [blame] | 1808 | } |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1809 | |
| 1810 | if (const ConstantDataSequential *CDS = |
| 1811 | dyn_cast<ConstantDataSequential>(Mask)) { |
| 1812 | unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements(); |
| 1813 | for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i) |
| 1814 | if (CDS->getElementAsInteger(i) >= V1Size*2) |
| 1815 | return false; |
| 1816 | return true; |
| 1817 | } |
| 1818 | |
| 1819 | // The bitcode reader can create a place holder for a forward reference |
| 1820 | // used as the shuffle mask. When this occurs, the shuffle mask will |
| 1821 | // fall into this case and fail. To avoid this error, do this bit of |
| 1822 | // ugliness to allow such a mask pass. |
| 1823 | if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask)) |
| 1824 | if (CE->getOpcode() == Instruction::UserOp1) |
| 1825 | return true; |
| 1826 | |
| 1827 | return false; |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1830 | /// getMaskValue - Return the index from the shuffle mask for the specified |
| 1831 | /// output result. This is either -1 if the element is undef or a number less |
| 1832 | /// than 2*numelements. |
Chris Lattner | cf12970 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1833 | int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) { |
| 1834 | assert(i < Mask->getType()->getVectorNumElements() && "Index out of range"); |
| 1835 | if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask)) |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1836 | return CDS->getElementAsInteger(i); |
Chris Lattner | cf12970 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1837 | Constant *C = Mask->getAggregateElement(i); |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1838 | if (isa<UndefValue>(C)) |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1839 | return -1; |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1840 | return cast<ConstantInt>(C)->getZExtValue(); |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1843 | /// getShuffleMask - Return the full mask for this instruction, where each |
| 1844 | /// element is the element number and undef's are returned as -1. |
Chris Lattner | cf12970 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1845 | void ShuffleVectorInst::getShuffleMask(Constant *Mask, |
| 1846 | SmallVectorImpl<int> &Result) { |
| 1847 | unsigned NumElts = Mask->getType()->getVectorNumElements(); |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1848 | |
Chris Lattner | cf12970 | 2012-01-26 02:51:13 +0000 | [diff] [blame] | 1849 | if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1850 | for (unsigned i = 0; i != NumElts; ++i) |
| 1851 | Result.push_back(CDS->getElementAsInteger(i)); |
| 1852 | return; |
| 1853 | } |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1854 | for (unsigned i = 0; i != NumElts; ++i) { |
| 1855 | Constant *C = Mask->getAggregateElement(i); |
| 1856 | Result.push_back(isa<UndefValue>(C) ? -1 : |
Chris Lattner | 3dbad403 | 2012-01-26 00:41:50 +0000 | [diff] [blame] | 1857 | cast<ConstantInt>(C)->getZExtValue()); |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1862 | //===----------------------------------------------------------------------===// |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1863 | // InsertValueInst Class |
| 1864 | //===----------------------------------------------------------------------===// |
| 1865 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1866 | void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs, |
| 1867 | const Twine &Name) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 1868 | assert(getNumOperands() == 2 && "NumOperands not initialized?"); |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1869 | |
| 1870 | // There's no fundamental reason why we require at least one index |
| 1871 | // (other than weirdness with &*IdxBegin being invalid; see |
| 1872 | // getelementptr's init routine for example). But there's no |
| 1873 | // present need to support it. |
| 1874 | assert(Idxs.size() > 0 && "InsertValueInst must have at least one index"); |
| 1875 | |
| 1876 | assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) == |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1877 | Val->getType() && "Inserted value must match indexed type!"); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1878 | Op<0>() = Agg; |
| 1879 | Op<1>() = Val; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1880 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1881 | Indices.append(Idxs.begin(), Idxs.end()); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1882 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
| 1885 | InsertValueInst::InsertValueInst(const InsertValueInst &IVI) |
Gabor Greif | e9408e6 | 2008-05-27 11:03:29 +0000 | [diff] [blame] | 1886 | : Instruction(IVI.getType(), InsertValue, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1887 | OperandTraits<InsertValueInst>::op_begin(this), 2), |
| 1888 | Indices(IVI.Indices) { |
Dan Gohman | d8ca05f | 2008-06-17 23:25:49 +0000 | [diff] [blame] | 1889 | Op<0>() = IVI.getOperand(0); |
| 1890 | Op<1>() = IVI.getOperand(1); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1891 | SubclassOptionalData = IVI.SubclassOptionalData; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
| 1894 | //===----------------------------------------------------------------------===// |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1895 | // ExtractValueInst Class |
| 1896 | //===----------------------------------------------------------------------===// |
| 1897 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1898 | void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 1899 | assert(getNumOperands() == 1 && "NumOperands not initialized?"); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1900 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1901 | // There's no fundamental reason why we require at least one index. |
| 1902 | // But there's no present need to support it. |
| 1903 | assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index"); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1904 | |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1905 | Indices.append(Idxs.begin(), Idxs.end()); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1906 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI) |
Gabor Greif | 21ba184 | 2008-06-06 20:28:12 +0000 | [diff] [blame] | 1910 | : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)), |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1911 | Indices(EVI.Indices) { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1912 | SubclassOptionalData = EVI.SubclassOptionalData; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1915 | // getIndexedType - Returns the type of the element that would be extracted |
| 1916 | // with an extractvalue instruction with the specified parameters. |
| 1917 | // |
| 1918 | // A null type is returned if the indices are invalid for the specified |
| 1919 | // pointer type. |
| 1920 | // |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1921 | Type *ExtractValueInst::getIndexedType(Type *Agg, |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1922 | ArrayRef<unsigned> Idxs) { |
Benjamin Kramer | 3ad5c96 | 2014-03-10 15:03:06 +0000 | [diff] [blame] | 1923 | for (unsigned Index : Idxs) { |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1924 | // We can't use CompositeType::indexValid(Index) here. |
| 1925 | // indexValid() always returns true for arrays because getelementptr allows |
| 1926 | // out-of-bounds indices. Since we don't allow those for extractvalue and |
| 1927 | // insertvalue we need to check array indexing manually. |
| 1928 | // Since the only other types we can index into are struct types it's just |
| 1929 | // as easy to check those manually as well. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1930 | if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) { |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1931 | if (Index >= AT->getNumElements()) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1932 | return nullptr; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1933 | } else if (StructType *ST = dyn_cast<StructType>(Agg)) { |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1934 | if (Index >= ST->getNumElements()) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1935 | return nullptr; |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1936 | } else { |
| 1937 | // Not a valid type to index into. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1938 | return nullptr; |
Frits van Bommel | 16ebe77 | 2010-12-05 20:50:26 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
| 1941 | Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1942 | } |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1943 | return const_cast<Type*>(Agg); |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1944 | } |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1945 | |
| 1946 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1947 | // BinaryOperator Class |
| 1948 | //===----------------------------------------------------------------------===// |
| 1949 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1950 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1951 | Type *Ty, const Twine &Name, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1952 | Instruction *InsertBefore) |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 1953 | : Instruction(Ty, iType, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1954 | OperandTraits<BinaryOperator>::op_begin(this), |
| 1955 | OperandTraits<BinaryOperator>::operands(this), |
| 1956 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1957 | Op<0>() = S1; |
| 1958 | Op<1>() = S2; |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 1959 | init(iType); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1960 | setName(Name); |
| 1961 | } |
| 1962 | |
| 1963 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1964 | Type *Ty, const Twine &Name, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1965 | BasicBlock *InsertAtEnd) |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 1966 | : Instruction(Ty, iType, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1967 | OperandTraits<BinaryOperator>::op_begin(this), |
| 1968 | OperandTraits<BinaryOperator>::operands(this), |
| 1969 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1970 | Op<0>() = S1; |
| 1971 | Op<1>() = S2; |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 1972 | init(iType); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1973 | setName(Name); |
| 1974 | } |
| 1975 | |
| 1976 | |
| 1977 | void BinaryOperator::init(BinaryOps iType) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1978 | Value *LHS = getOperand(0), *RHS = getOperand(1); |
Jeffrey Yasskin | 9b43f33 | 2010-12-23 00:58:24 +0000 | [diff] [blame] | 1979 | (void)LHS; (void)RHS; // Silence warnings. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1980 | assert(LHS->getType() == RHS->getType() && |
| 1981 | "Binary operator operand types must match!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1982 | #ifndef NDEBUG |
| 1983 | switch (iType) { |
| 1984 | case Add: case Sub: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1985 | case Mul: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1986 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1987 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1988 | assert(getType()->isIntOrIntVectorTy() && |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1989 | "Tried to create an integer operation on a non-integer type!"); |
| 1990 | break; |
| 1991 | case FAdd: case FSub: |
| 1992 | case FMul: |
| 1993 | assert(getType() == LHS->getType() && |
| 1994 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1995 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1996 | "Tried to create a floating-point operation on a " |
| 1997 | "non-floating-point type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1998 | break; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1999 | case UDiv: |
| 2000 | case SDiv: |
| 2001 | assert(getType() == LHS->getType() && |
| 2002 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2003 | assert((getType()->isIntegerTy() || (getType()->isVectorTy() && |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2004 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2005 | "Incorrect operand type (not integer) for S/UDIV"); |
| 2006 | break; |
| 2007 | case FDiv: |
| 2008 | assert(getType() == LHS->getType() && |
| 2009 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2010 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2011 | "Incorrect operand type (not floating point) for FDIV"); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 2012 | break; |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2013 | case URem: |
| 2014 | case SRem: |
| 2015 | assert(getType() == LHS->getType() && |
| 2016 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2017 | assert((getType()->isIntegerTy() || (getType()->isVectorTy() && |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2018 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2019 | "Incorrect operand type (not integer) for S/UREM"); |
| 2020 | break; |
| 2021 | case FRem: |
| 2022 | assert(getType() == LHS->getType() && |
| 2023 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2024 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2025 | "Incorrect operand type (not floating point) for FREM"); |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 2026 | break; |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2027 | case Shl: |
| 2028 | case LShr: |
| 2029 | case AShr: |
| 2030 | assert(getType() == LHS->getType() && |
| 2031 | "Shift operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2032 | assert((getType()->isIntegerTy() || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2033 | (getType()->isVectorTy() && |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2034 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Nate Begeman | fecbc8c | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 2035 | "Tried to create a shift operation on a non-integral type!"); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 2036 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2037 | case And: case Or: |
| 2038 | case Xor: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2039 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2040 | "Logical operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2041 | assert((getType()->isIntegerTy() || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2042 | (getType()->isVectorTy() && |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2043 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Misha Brukman | 3852f65 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 2044 | "Tried to create a logical operation on a non-integral type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2045 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2046 | default: |
| 2047 | break; |
| 2048 | } |
| 2049 | #endif |
| 2050 | } |
| 2051 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2052 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2053 | const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2054 | Instruction *InsertBefore) { |
| 2055 | assert(S1->getType() == S2->getType() && |
| 2056 | "Cannot create binary operator with two operands of differing type!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2057 | return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2060 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2061 | const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2062 | BasicBlock *InsertAtEnd) { |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2063 | BinaryOperator *Res = Create(Op, S1, S2, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2064 | InsertAtEnd->getInstList().push_back(Res); |
| 2065 | return Res; |
| 2066 | } |
| 2067 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2068 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2069 | Instruction *InsertBefore) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2070 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 2071 | return new BinaryOperator(Instruction::Sub, |
| 2072 | zero, Op, |
| 2073 | Op->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2076 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2077 | BasicBlock *InsertAtEnd) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2078 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 2079 | return new BinaryOperator(Instruction::Sub, |
| 2080 | zero, Op, |
| 2081 | Op->getType(), Name, InsertAtEnd); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
Dan Gohman | 4ab4420 | 2009-12-18 02:58:50 +0000 | [diff] [blame] | 2084 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, |
| 2085 | Instruction *InsertBefore) { |
| 2086 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2087 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore); |
| 2088 | } |
| 2089 | |
| 2090 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, |
| 2091 | BasicBlock *InsertAtEnd) { |
| 2092 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2093 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd); |
| 2094 | } |
| 2095 | |
Duncan Sands | fa5f596 | 2010-02-02 12:53:04 +0000 | [diff] [blame] | 2096 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, |
| 2097 | Instruction *InsertBefore) { |
| 2098 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2099 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore); |
| 2100 | } |
| 2101 | |
| 2102 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, |
| 2103 | BasicBlock *InsertAtEnd) { |
| 2104 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 2105 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd); |
| 2106 | } |
| 2107 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2108 | BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2109 | Instruction *InsertBefore) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2110 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2111 | return new BinaryOperator(Instruction::FSub, zero, Op, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2112 | Op->getType(), Name, InsertBefore); |
| 2113 | } |
| 2114 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2115 | BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2116 | BasicBlock *InsertAtEnd) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 2117 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2118 | return new BinaryOperator(Instruction::FSub, zero, Op, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2119 | Op->getType(), Name, InsertAtEnd); |
| 2120 | } |
| 2121 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2122 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2123 | Instruction *InsertBefore) { |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2124 | Constant *C = Constant::getAllOnesValue(Op->getType()); |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 2125 | return new BinaryOperator(Instruction::Xor, Op, C, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2126 | Op->getType(), Name, InsertBefore); |
| 2127 | } |
| 2128 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 2129 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2130 | BasicBlock *InsertAtEnd) { |
Chris Lattner | 47a86bd | 2012-01-25 06:02:56 +0000 | [diff] [blame] | 2131 | Constant *AllOnes = Constant::getAllOnesValue(Op->getType()); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 2132 | return new BinaryOperator(Instruction::Xor, Op, AllOnes, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2133 | Op->getType(), Name, InsertAtEnd); |
| 2134 | } |
| 2135 | |
| 2136 | |
| 2137 | // isConstantAllOnes - Helper function for several functions below |
| 2138 | static inline bool isConstantAllOnes(const Value *V) { |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 2139 | if (const Constant *C = dyn_cast<Constant>(V)) |
| 2140 | return C->isAllOnesValue(); |
Chris Lattner | 1edec38 | 2007-06-15 06:04:24 +0000 | [diff] [blame] | 2141 | return false; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2142 | } |
| 2143 | |
Owen Anderson | bb2501b | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 2144 | bool BinaryOperator::isNeg(const Value *V) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2145 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 2146 | if (Bop->getOpcode() == Instruction::Sub) |
Owen Anderson | bb2501b | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 2147 | if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) |
| 2148 | return C->isNegativeZeroValue(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2149 | return false; |
| 2150 | } |
| 2151 | |
Shuxin Yang | f0537ab | 2013-01-09 00:13:41 +0000 | [diff] [blame] | 2152 | bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) { |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2153 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 2154 | if (Bop->getOpcode() == Instruction::FSub) |
Shuxin Yang | f0537ab | 2013-01-09 00:13:41 +0000 | [diff] [blame] | 2155 | if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) { |
| 2156 | if (!IgnoreZeroSign) |
| 2157 | IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros(); |
| 2158 | return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue(); |
| 2159 | } |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2160 | return false; |
| 2161 | } |
| 2162 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2163 | bool BinaryOperator::isNot(const Value *V) { |
| 2164 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 2165 | return (Bop->getOpcode() == Instruction::Xor && |
| 2166 | (isConstantAllOnes(Bop->getOperand(1)) || |
| 2167 | isConstantAllOnes(Bop->getOperand(0)))); |
| 2168 | return false; |
| 2169 | } |
| 2170 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 2171 | Value *BinaryOperator::getNegArgument(Value *BinOp) { |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 2172 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2173 | } |
| 2174 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 2175 | const Value *BinaryOperator::getNegArgument(const Value *BinOp) { |
| 2176 | return getNegArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2179 | Value *BinaryOperator::getFNegArgument(Value *BinOp) { |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2180 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
| 2181 | } |
| 2182 | |
| 2183 | const Value *BinaryOperator::getFNegArgument(const Value *BinOp) { |
| 2184 | return getFNegArgument(const_cast<Value*>(BinOp)); |
| 2185 | } |
| 2186 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 2187 | Value *BinaryOperator::getNotArgument(Value *BinOp) { |
| 2188 | assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!"); |
| 2189 | BinaryOperator *BO = cast<BinaryOperator>(BinOp); |
| 2190 | Value *Op0 = BO->getOperand(0); |
| 2191 | Value *Op1 = BO->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2192 | if (isConstantAllOnes(Op0)) return Op1; |
| 2193 | |
| 2194 | assert(isConstantAllOnes(Op1)); |
| 2195 | return Op0; |
| 2196 | } |
| 2197 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 2198 | const Value *BinaryOperator::getNotArgument(const Value *BinOp) { |
| 2199 | return getNotArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
| 2202 | |
| 2203 | // swapOperands - Exchange the two operands to this instruction. This |
| 2204 | // instruction is safe to use on any binary instruction and does not |
| 2205 | // modify the semantics of the instruction. If the instruction is |
| 2206 | // order dependent (SetLT f.e.) the opcode is changed. |
| 2207 | // |
| 2208 | bool BinaryOperator::swapOperands() { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2209 | if (!isCommutative()) |
| 2210 | return true; // Can't commute operands |
Gabor Greif | 5ef7404 | 2008-05-13 22:51:52 +0000 | [diff] [blame] | 2211 | Op<0>().swap(Op<1>()); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2212 | return false; |
| 2213 | } |
| 2214 | |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2215 | void BinaryOperator::setHasNoUnsignedWrap(bool b) { |
| 2216 | cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b); |
| 2217 | } |
| 2218 | |
| 2219 | void BinaryOperator::setHasNoSignedWrap(bool b) { |
| 2220 | cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b); |
| 2221 | } |
| 2222 | |
| 2223 | void BinaryOperator::setIsExact(bool b) { |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 2224 | cast<PossiblyExactOperator>(this)->setIsExact(b); |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 2225 | } |
| 2226 | |
Nick Lewycky | 28a5f25 | 2009-09-27 21:33:04 +0000 | [diff] [blame] | 2227 | bool BinaryOperator::hasNoUnsignedWrap() const { |
| 2228 | return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap(); |
| 2229 | } |
| 2230 | |
| 2231 | bool BinaryOperator::hasNoSignedWrap() const { |
| 2232 | return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap(); |
| 2233 | } |
| 2234 | |
| 2235 | bool BinaryOperator::isExact() const { |
Chris Lattner | 35315d0 | 2011-02-06 21:44:57 +0000 | [diff] [blame] | 2236 | return cast<PossiblyExactOperator>(this)->isExact(); |
Nick Lewycky | 28a5f25 | 2009-09-27 21:33:04 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
Sanjay Patel | a982d99 | 2014-09-03 01:06:50 +0000 | [diff] [blame] | 2239 | void BinaryOperator::copyIRFlags(const Value *V) { |
Sanjay Patel | 5ad239e | 2014-09-01 18:44:57 +0000 | [diff] [blame] | 2240 | // Copy the wrapping flags. |
| 2241 | if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) { |
| 2242 | setHasNoSignedWrap(OB->hasNoSignedWrap()); |
| 2243 | setHasNoUnsignedWrap(OB->hasNoUnsignedWrap()); |
| 2244 | } |
| 2245 | |
| 2246 | // Copy the exact flag. |
| 2247 | if (auto *PE = dyn_cast<PossiblyExactOperator>(V)) |
| 2248 | setIsExact(PE->isExact()); |
| 2249 | |
| 2250 | // Copy the fast-math flags. |
| 2251 | if (auto *FP = dyn_cast<FPMathOperator>(V)) |
Sanjay Patel | b2325b9 | 2014-09-02 20:03:00 +0000 | [diff] [blame] | 2252 | copyFastMathFlags(FP->getFastMathFlags()); |
Sanjay Patel | 5ad239e | 2014-09-01 18:44:57 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
Sanjay Patel | a982d99 | 2014-09-03 01:06:50 +0000 | [diff] [blame] | 2255 | void BinaryOperator::andIRFlags(const Value *V) { |
| 2256 | if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) { |
| 2257 | setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap()); |
| 2258 | setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap()); |
| 2259 | } |
| 2260 | |
| 2261 | if (auto *PE = dyn_cast<PossiblyExactOperator>(V)) |
| 2262 | setIsExact(isExact() & PE->isExact()); |
| 2263 | |
| 2264 | if (auto *FP = dyn_cast<FPMathOperator>(V)) { |
| 2265 | FastMathFlags FM = getFastMathFlags(); |
| 2266 | FM &= FP->getFastMathFlags(); |
| 2267 | copyFastMathFlags(FM); |
| 2268 | } |
| 2269 | } |
| 2270 | |
| 2271 | |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2272 | //===----------------------------------------------------------------------===// |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2273 | // FPMathOperator Class |
| 2274 | //===----------------------------------------------------------------------===// |
| 2275 | |
| 2276 | /// getFPAccuracy - Get the maximum error permitted by this operation in ULPs. |
| 2277 | /// An accuracy of 0.0 means that the operation should be performed with the |
Duncan Sands | 9af6298 | 2012-04-16 19:39:33 +0000 | [diff] [blame] | 2278 | /// default precision. |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2279 | float FPMathOperator::getFPAccuracy() const { |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 2280 | const MDNode *MD = |
| 2281 | cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath); |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2282 | if (!MD) |
| 2283 | return 0.0; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 2284 | ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0)); |
Duncan Sands | 9af6298 | 2012-04-16 19:39:33 +0000 | [diff] [blame] | 2285 | return Accuracy->getValueAPF().convertToFloat(); |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 2286 | } |
| 2287 | |
| 2288 | |
| 2289 | //===----------------------------------------------------------------------===// |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2290 | // CastInst Class |
| 2291 | //===----------------------------------------------------------------------===// |
| 2292 | |
David Blaikie | 3a15e14 | 2011-12-01 08:00:17 +0000 | [diff] [blame] | 2293 | void CastInst::anchor() {} |
| 2294 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2295 | // Just determine if this cast only deals with integral->integral conversion. |
| 2296 | bool CastInst::isIntegerCast() const { |
| 2297 | switch (getOpcode()) { |
| 2298 | default: return false; |
| 2299 | case Instruction::ZExt: |
| 2300 | case Instruction::SExt: |
| 2301 | case Instruction::Trunc: |
| 2302 | return true; |
| 2303 | case Instruction::BitCast: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2304 | return getOperand(0)->getType()->isIntegerTy() && |
| 2305 | getType()->isIntegerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2306 | } |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2309 | bool CastInst::isLosslessCast() const { |
| 2310 | // Only BitCast can be lossless, exit fast if we're not BitCast |
| 2311 | if (getOpcode() != Instruction::BitCast) |
| 2312 | return false; |
| 2313 | |
| 2314 | // Identity cast is always lossless |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2315 | Type* SrcTy = getOperand(0)->getType(); |
| 2316 | Type* DstTy = getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2317 | if (SrcTy == DstTy) |
| 2318 | return true; |
| 2319 | |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 2320 | // Pointer to pointer is always lossless. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2321 | if (SrcTy->isPointerTy()) |
| 2322 | return DstTy->isPointerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2323 | return false; // Other types have no identity values |
| 2324 | } |
| 2325 | |
| 2326 | /// This function determines if the CastInst does not require any bits to be |
| 2327 | /// changed in order to effect the cast. Essentially, it identifies cases where |
| 2328 | /// no code gen is necessary for the cast, hence the name no-op cast. For |
| 2329 | /// example, the following are all no-op casts: |
Dan Gohman | e9bc2ba | 2008-05-12 16:34:30 +0000 | [diff] [blame] | 2330 | /// # bitcast i32* %x to i8* |
| 2331 | /// # bitcast <2 x i32> %x to <4 x i16> |
| 2332 | /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2333 | /// @brief Determine if the described cast is a no-op. |
| 2334 | bool CastInst::isNoopCast(Instruction::CastOps Opcode, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2335 | Type *SrcTy, |
| 2336 | Type *DestTy, |
| 2337 | Type *IntPtrTy) { |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2338 | switch (Opcode) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2339 | default: llvm_unreachable("Invalid CastOp"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2340 | case Instruction::Trunc: |
| 2341 | case Instruction::ZExt: |
| 2342 | case Instruction::SExt: |
| 2343 | case Instruction::FPTrunc: |
| 2344 | case Instruction::FPExt: |
| 2345 | case Instruction::UIToFP: |
| 2346 | case Instruction::SIToFP: |
| 2347 | case Instruction::FPToUI: |
| 2348 | case Instruction::FPToSI: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2349 | case Instruction::AddrSpaceCast: |
| 2350 | // TODO: Target informations may give a more accurate answer here. |
| 2351 | return false; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2352 | case Instruction::BitCast: |
| 2353 | return true; // BitCast never modifies bits. |
| 2354 | case Instruction::PtrToInt: |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2355 | return IntPtrTy->getScalarSizeInBits() == |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2356 | DestTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2357 | case Instruction::IntToPtr: |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2358 | return IntPtrTy->getScalarSizeInBits() == |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2359 | SrcTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2360 | } |
| 2361 | } |
| 2362 | |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2363 | /// @brief Determine if a cast is a no-op. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2364 | bool CastInst::isNoopCast(Type *IntPtrTy) const { |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 2365 | return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy); |
| 2366 | } |
| 2367 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2368 | bool CastInst::isNoopCast(const DataLayout &DL) const { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 2369 | Type *PtrOpTy = nullptr; |
Matt Arsenault | a236ea5 | 2014-03-06 17:33:55 +0000 | [diff] [blame] | 2370 | if (getOpcode() == Instruction::PtrToInt) |
| 2371 | PtrOpTy = getOperand(0)->getType(); |
| 2372 | else if (getOpcode() == Instruction::IntToPtr) |
| 2373 | PtrOpTy = getType(); |
| 2374 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2375 | Type *IntPtrTy = |
| 2376 | PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0); |
Matt Arsenault | a236ea5 | 2014-03-06 17:33:55 +0000 | [diff] [blame] | 2377 | |
| 2378 | return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy); |
| 2379 | } |
| 2380 | |
| 2381 | /// This function determines if a pair of casts can be eliminated and what |
| 2382 | /// opcode should be used in the elimination. This assumes that there are two |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2383 | /// instructions like this: |
| 2384 | /// * %F = firstOpcode SrcTy %x to MidTy |
| 2385 | /// * %S = secondOpcode MidTy %F to DstTy |
| 2386 | /// The function returns a resultOpcode so these two casts can be replaced with: |
| 2387 | /// * %Replacement = resultOpcode %SrcTy %x to DstTy |
| 2388 | /// If no such cast is permited, the function returns 0. |
| 2389 | unsigned CastInst::isEliminableCastPair( |
| 2390 | Instruction::CastOps firstOp, Instruction::CastOps secondOp, |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2391 | Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy, |
| 2392 | Type *DstIntPtrTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2393 | // Define the 144 possibilities for these two cast instructions. The values |
| 2394 | // in this matrix determine what to do in a given situation and select the |
| 2395 | // case in the switch below. The rows correspond to firstOp, the columns |
| 2396 | // correspond to secondOp. In looking at the table below, keep in mind |
| 2397 | // the following cast properties: |
| 2398 | // |
| 2399 | // Size Compare Source Destination |
| 2400 | // Operator Src ? Size Type Sign Type Sign |
| 2401 | // -------- ------------ ------------------- --------------------- |
| 2402 | // TRUNC > Integer Any Integral Any |
| 2403 | // ZEXT < Integral Unsigned Integer Any |
| 2404 | // SEXT < Integral Signed Integer Any |
| 2405 | // FPTOUI n/a FloatPt n/a Integral Unsigned |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2406 | // FPTOSI n/a FloatPt n/a Integral Signed |
| 2407 | // UITOFP n/a Integral Unsigned FloatPt n/a |
| 2408 | // SITOFP n/a Integral Signed FloatPt n/a |
| 2409 | // FPTRUNC > FloatPt n/a FloatPt n/a |
| 2410 | // FPEXT < FloatPt n/a FloatPt n/a |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2411 | // PTRTOINT n/a Pointer n/a Integral Unsigned |
| 2412 | // INTTOPTR n/a Integral Unsigned Pointer n/a |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2413 | // BITCAST = FirstClass n/a FirstClass n/a |
| 2414 | // ADDRSPCST n/a Pointer n/a Pointer n/a |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2415 | // |
| 2416 | // NOTE: some transforms are safe, but we consider them to be non-profitable. |
Dan Gohman | 4fe64de | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 2417 | // For example, we could merge "fptoui double to i32" + "zext i32 to i64", |
| 2418 | // into "fptoui double to i64", but this loses information about the range |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2419 | // of the produced value (we no longer know the top-part is all zeros). |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2420 | // Further this conversion is often much more expensive for typical hardware, |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2421 | // and causes issues when building libgcc. We disallow fptosi+sext for the |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 2422 | // same reason. |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2423 | const unsigned numCastOps = |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2424 | Instruction::CastOpsEnd - Instruction::CastOpsBegin; |
| 2425 | static const uint8_t CastResults[numCastOps][numCastOps] = { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2426 | // T F F U S F F P I B A -+ |
| 2427 | // R Z S P P I I T P 2 N T S | |
| 2428 | // U E E 2 2 2 2 R E I T C C +- secondOp |
| 2429 | // N X X U S F F N X N 2 V V | |
| 2430 | // C T T I I P P C T T P T T -+ |
| 2431 | { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+ |
Fiona Glaser | 0d41db1 | 2015-04-21 00:05:41 +0000 | [diff] [blame] | 2432 | { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt | |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2433 | { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt | |
| 2434 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI | |
| 2435 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI | |
| 2436 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp |
| 2437 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP | |
Ahmed Bougacha | 0ea9d1e | 2015-05-29 00:04:30 +0000 | [diff] [blame] | 2438 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc | |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2439 | { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt | |
| 2440 | { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt | |
| 2441 | { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr | |
| 2442 | { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast | |
| 2443 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+ |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2444 | }; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2445 | |
Chris Lattner | 25eea4d | 2010-07-12 01:19:22 +0000 | [diff] [blame] | 2446 | // If either of the casts are a bitcast from scalar to vector, disallow the |
Nadav Rotem | 5fc81ff | 2011-08-29 19:58:36 +0000 | [diff] [blame] | 2447 | // merging. However, bitcast of A->B->A are allowed. |
| 2448 | bool isFirstBitcast = (firstOp == Instruction::BitCast); |
| 2449 | bool isSecondBitcast = (secondOp == Instruction::BitCast); |
| 2450 | bool chainedBitcast = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast); |
| 2451 | |
| 2452 | // Check if any of the bitcasts convert scalars<->vectors. |
| 2453 | if ((isFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) || |
| 2454 | (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy))) |
| 2455 | // Unless we are bitcasing to the original type, disallow optimizations. |
| 2456 | if (!chainedBitcast) return 0; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2457 | |
| 2458 | int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] |
| 2459 | [secondOp-Instruction::CastOpsBegin]; |
| 2460 | switch (ElimCase) { |
| 2461 | case 0: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2462 | // Categorically disallowed. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2463 | return 0; |
| 2464 | case 1: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2465 | // Allowed, use first cast's opcode. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2466 | return firstOp; |
| 2467 | case 2: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2468 | // Allowed, use second cast's opcode. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2469 | return secondOp; |
| 2470 | case 3: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2471 | // No-op cast in second op implies firstOp as long as the DestTy |
Mon P Wang | e04b456 | 2010-01-23 04:35:57 +0000 | [diff] [blame] | 2472 | // is integer and we are not converting between a vector and a |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 2473 | // non-vector type. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2474 | if (!SrcTy->isVectorTy() && DstTy->isIntegerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2475 | return firstOp; |
| 2476 | return 0; |
| 2477 | case 4: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2478 | // No-op cast in second op implies firstOp as long as the DestTy |
Chris Lattner | 531732b | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2479 | // is floating point. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2480 | if (DstTy->isFloatingPointTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2481 | return firstOp; |
| 2482 | return 0; |
| 2483 | case 5: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2484 | // No-op cast in first op implies secondOp as long as the SrcTy |
Chris Lattner | 531732b | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2485 | // is an integer. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2486 | if (SrcTy->isIntegerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2487 | return secondOp; |
| 2488 | return 0; |
| 2489 | case 6: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2490 | // No-op cast in first op implies secondOp as long as the SrcTy |
Chris Lattner | 531732b | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2491 | // is a floating point. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2492 | if (SrcTy->isFloatingPointTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2493 | return secondOp; |
| 2494 | return 0; |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2495 | case 7: { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2496 | // Cannot simplify if address spaces are different! |
| 2497 | if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) |
| 2498 | return 0; |
| 2499 | |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2500 | unsigned MidSize = MidTy->getScalarSizeInBits(); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2501 | // We can still fold this without knowing the actual sizes as long we |
| 2502 | // know that the intermediate pointer is the largest possible |
| 2503 | // pointer size. |
| 2504 | // FIXME: Is this always true? |
| 2505 | if (MidSize == 64) |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2506 | return Instruction::BitCast; |
| 2507 | |
| 2508 | // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size. |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2509 | if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy) |
Dan Gohman | 9413de1 | 2009-07-21 23:19:40 +0000 | [diff] [blame] | 2510 | return 0; |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2511 | unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2512 | if (MidSize >= PtrSize) |
| 2513 | return Instruction::BitCast; |
| 2514 | return 0; |
| 2515 | } |
| 2516 | case 8: { |
| 2517 | // ext, trunc -> bitcast, if the SrcTy and DstTy are same size |
| 2518 | // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) |
| 2519 | // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2520 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); |
| 2521 | unsigned DstSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2522 | if (SrcSize == DstSize) |
| 2523 | return Instruction::BitCast; |
| 2524 | else if (SrcSize < DstSize) |
| 2525 | return firstOp; |
| 2526 | return secondOp; |
| 2527 | } |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2528 | case 9: |
| 2529 | // zext, sext -> zext, because sext can't sign extend after zext |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2530 | return Instruction::ZExt; |
| 2531 | case 10: |
| 2532 | // fpext followed by ftrunc is allowed if the bit size returned to is |
| 2533 | // the same as the original, in which case its just a bitcast |
| 2534 | if (SrcTy == DstTy) |
| 2535 | return Instruction::BitCast; |
| 2536 | return 0; // If the types are not the same we can't eliminate it. |
Matt Arsenault | 130e0ef | 2013-07-30 22:27:10 +0000 | [diff] [blame] | 2537 | case 11: { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2538 | // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2539 | if (!MidIntPtrTy) |
Dan Gohman | 9413de1 | 2009-07-21 23:19:40 +0000 | [diff] [blame] | 2540 | return 0; |
Duncan Sands | e2395dc | 2012-10-30 16:03:32 +0000 | [diff] [blame] | 2541 | unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits(); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2542 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); |
| 2543 | unsigned DstSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2544 | if (SrcSize <= PtrSize && SrcSize == DstSize) |
| 2545 | return Instruction::BitCast; |
| 2546 | return 0; |
| 2547 | } |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2548 | case 12: { |
| 2549 | // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS |
| 2550 | // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS |
| 2551 | if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace()) |
| 2552 | return Instruction::AddrSpaceCast; |
| 2553 | return Instruction::BitCast; |
| 2554 | } |
| 2555 | case 13: |
| 2556 | // FIXME: this state can be merged with (1), but the following assert |
| 2557 | // is useful to check the correcteness of the sequence due to semantic |
| 2558 | // change of bitcast. |
| 2559 | assert( |
| 2560 | SrcTy->isPtrOrPtrVectorTy() && |
| 2561 | MidTy->isPtrOrPtrVectorTy() && |
| 2562 | DstTy->isPtrOrPtrVectorTy() && |
| 2563 | SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() && |
| 2564 | MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() && |
| 2565 | "Illegal addrspacecast, bitcast sequence!"); |
| 2566 | // Allowed, use first cast's opcode |
| 2567 | return firstOp; |
| 2568 | case 14: |
Jingyue Wu | 77145d9 | 2014-06-06 21:52:55 +0000 | [diff] [blame] | 2569 | // bitcast, addrspacecast -> addrspacecast if the element type of |
| 2570 | // bitcast's source is the same as that of addrspacecast's destination. |
| 2571 | if (SrcTy->getPointerElementType() == DstTy->getPointerElementType()) |
| 2572 | return Instruction::AddrSpaceCast; |
| 2573 | return 0; |
| 2574 | |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2575 | case 15: |
| 2576 | // FIXME: this state can be merged with (1), but the following assert |
| 2577 | // is useful to check the correcteness of the sequence due to semantic |
| 2578 | // change of bitcast. |
| 2579 | assert( |
| 2580 | SrcTy->isIntOrIntVectorTy() && |
| 2581 | MidTy->isPtrOrPtrVectorTy() && |
| 2582 | DstTy->isPtrOrPtrVectorTy() && |
| 2583 | MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() && |
| 2584 | "Illegal inttoptr, bitcast sequence!"); |
| 2585 | // Allowed, use first cast's opcode |
| 2586 | return firstOp; |
| 2587 | case 16: |
| 2588 | // FIXME: this state can be merged with (2), but the following assert |
| 2589 | // is useful to check the correcteness of the sequence due to semantic |
| 2590 | // change of bitcast. |
| 2591 | assert( |
| 2592 | SrcTy->isPtrOrPtrVectorTy() && |
| 2593 | MidTy->isPtrOrPtrVectorTy() && |
| 2594 | DstTy->isIntOrIntVectorTy() && |
| 2595 | SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() && |
| 2596 | "Illegal bitcast, ptrtoint sequence!"); |
| 2597 | // Allowed, use second cast's opcode |
| 2598 | return secondOp; |
Fiona Glaser | 0d41db1 | 2015-04-21 00:05:41 +0000 | [diff] [blame] | 2599 | case 17: |
| 2600 | // (sitofp (zext x)) -> (uitofp x) |
| 2601 | return Instruction::UIToFP; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2602 | case 99: |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2603 | // Cast combination can't happen (error in input). This is for all cases |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2604 | // where the MidTy is not the same for the two cast instructions. |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2605 | llvm_unreachable("Invalid Cast Combination"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2606 | default: |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 2607 | llvm_unreachable("Error in CastResults table!!!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2608 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2611 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2612 | const Twine &Name, Instruction *InsertBefore) { |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2613 | assert(castIsValid(op, S, Ty) && "Invalid cast!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2614 | // Construct and return the appropriate CastInst subclass |
| 2615 | switch (op) { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2616 | case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); |
| 2617 | case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); |
| 2618 | case SExt: return new SExtInst (S, Ty, Name, InsertBefore); |
| 2619 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); |
| 2620 | case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); |
| 2621 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); |
| 2622 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); |
| 2623 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); |
| 2624 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); |
| 2625 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); |
| 2626 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); |
| 2627 | case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); |
| 2628 | case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore); |
| 2629 | default: llvm_unreachable("Invalid opcode provided"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2630 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2633 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2634 | const Twine &Name, BasicBlock *InsertAtEnd) { |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2635 | assert(castIsValid(op, S, Ty) && "Invalid cast!"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2636 | // Construct and return the appropriate CastInst subclass |
| 2637 | switch (op) { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2638 | case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd); |
| 2639 | case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd); |
| 2640 | case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd); |
| 2641 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd); |
| 2642 | case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd); |
| 2643 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd); |
| 2644 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd); |
| 2645 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd); |
| 2646 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd); |
| 2647 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); |
| 2648 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); |
| 2649 | case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); |
| 2650 | case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd); |
| 2651 | default: llvm_unreachable("Invalid opcode provided"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2652 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2653 | } |
| 2654 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2655 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2656 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2657 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2658 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2659 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2660 | return Create(Instruction::ZExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2661 | } |
| 2662 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2663 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2664 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2665 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2666 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2667 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2668 | return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2669 | } |
| 2670 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2671 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2672 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2673 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2674 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2675 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2676 | return Create(Instruction::SExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2677 | } |
| 2678 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2679 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2680 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2681 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2682 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2683 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2684 | return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2687 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2688 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2689 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2690 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2691 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2692 | return Create(Instruction::Trunc, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2695 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2696 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2697 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2698 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2699 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2700 | return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2703 | CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2704 | const Twine &Name, |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2705 | BasicBlock *InsertAtEnd) { |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2706 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2707 | assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && |
| 2708 | "Invalid cast"); |
| 2709 | assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast"); |
Richard Trieu | 8dc4323 | 2013-07-31 04:07:28 +0000 | [diff] [blame] | 2710 | assert((!Ty->isVectorTy() || |
| 2711 | Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2712 | "Invalid cast"); |
| 2713 | |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2714 | if (Ty->isIntOrIntVectorTy()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2715 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2716 | |
Matt Arsenault | 740980e | 2014-07-14 17:24:35 +0000 | [diff] [blame] | 2717 | return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2718 | } |
| 2719 | |
| 2720 | /// @brief Create a BitCast or a PtrToInt cast instruction |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2721 | CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty, |
| 2722 | const Twine &Name, |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2723 | Instruction *InsertBefore) { |
Evgeniy Stepanov | c9bd35b | 2013-01-15 16:43:00 +0000 | [diff] [blame] | 2724 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2725 | assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2726 | "Invalid cast"); |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2727 | assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast"); |
Richard Trieu | 8dc4323 | 2013-07-31 04:07:28 +0000 | [diff] [blame] | 2728 | assert((!Ty->isVectorTy() || |
| 2729 | Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) && |
Matt Arsenault | 065ced9 | 2013-07-31 00:17:33 +0000 | [diff] [blame] | 2730 | "Invalid cast"); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2731 | |
Evgeniy Stepanov | c9bd35b | 2013-01-15 16:43:00 +0000 | [diff] [blame] | 2732 | if (Ty->isIntOrIntVectorTy()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2733 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2734 | |
Matt Arsenault | 740980e | 2014-07-14 17:24:35 +0000 | [diff] [blame] | 2735 | return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore); |
| 2736 | } |
| 2737 | |
| 2738 | CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast( |
| 2739 | Value *S, Type *Ty, |
| 2740 | const Twine &Name, |
| 2741 | BasicBlock *InsertAtEnd) { |
| 2742 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2743 | assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2744 | |
| 2745 | if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) |
| 2746 | return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd); |
| 2747 | |
| 2748 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2749 | } |
| 2750 | |
| 2751 | CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast( |
| 2752 | Value *S, Type *Ty, |
| 2753 | const Twine &Name, |
| 2754 | Instruction *InsertBefore) { |
| 2755 | assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2756 | assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast"); |
| 2757 | |
| 2758 | if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace()) |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 2759 | return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore); |
| 2760 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2761 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2764 | CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty, |
| 2765 | const Twine &Name, |
| 2766 | Instruction *InsertBefore) { |
| 2767 | if (S->getType()->isPointerTy() && Ty->isIntegerTy()) |
| 2768 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); |
| 2769 | if (S->getType()->isIntegerTy() && Ty->isPointerTy()) |
| 2770 | return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore); |
| 2771 | |
| 2772 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2773 | } |
| 2774 | |
Matt Arsenault | 740980e | 2014-07-14 17:24:35 +0000 | [diff] [blame] | 2775 | CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2776 | bool isSigned, const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2777 | Instruction *InsertBefore) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2778 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && |
Chris Lattner | 5370ae7 | 2010-01-10 20:21:42 +0000 | [diff] [blame] | 2779 | "Invalid integer cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2780 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2781 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2782 | Instruction::CastOps opcode = |
| 2783 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2784 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2785 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2786 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2787 | } |
| 2788 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2789 | CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2790 | bool isSigned, const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2791 | BasicBlock *InsertAtEnd) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2792 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2793 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2794 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2795 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2796 | Instruction::CastOps opcode = |
| 2797 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2798 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2799 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2800 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2801 | } |
| 2802 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2803 | CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2804 | const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2805 | Instruction *InsertBefore) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2806 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2807 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2808 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2809 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2810 | Instruction::CastOps opcode = |
| 2811 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2812 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2813 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2814 | } |
| 2815 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2816 | CastInst *CastInst::CreateFPCast(Value *C, Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2817 | const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2818 | BasicBlock *InsertAtEnd) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2819 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2820 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2821 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2822 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2823 | Instruction::CastOps opcode = |
| 2824 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2825 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2826 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2829 | // Check whether it is valid to call getCastOpcode for these types. |
| 2830 | // This routine must be kept in sync with getCastOpcode. |
| 2831 | bool CastInst::isCastable(Type *SrcTy, Type *DestTy) { |
| 2832 | if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) |
| 2833 | return false; |
| 2834 | |
| 2835 | if (SrcTy == DestTy) |
| 2836 | return true; |
| 2837 | |
| 2838 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) |
| 2839 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) |
| 2840 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 2841 | // An element by element cast. Valid if casting the elements is valid. |
| 2842 | SrcTy = SrcVecTy->getElementType(); |
| 2843 | DestTy = DestVecTy->getElementType(); |
| 2844 | } |
| 2845 | |
| 2846 | // Get the bit sizes, we'll need these |
| 2847 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2848 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2849 | |
| 2850 | // Run through the possibilities ... |
| 2851 | if (DestTy->isIntegerTy()) { // Casting to integral |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2852 | if (SrcTy->isIntegerTy()) // Casting from integral |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2853 | return true; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2854 | if (SrcTy->isFloatingPointTy()) // Casting from floating pt |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2855 | return true; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2856 | if (SrcTy->isVectorTy()) // Casting from vector |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2857 | return DestBits == SrcBits; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2858 | // Casting from something else |
| 2859 | return SrcTy->isPointerTy(); |
| 2860 | } |
| 2861 | if (DestTy->isFloatingPointTy()) { // Casting to floating pt |
| 2862 | if (SrcTy->isIntegerTy()) // Casting from integral |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2863 | return true; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2864 | if (SrcTy->isFloatingPointTy()) // Casting from floating pt |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2865 | return true; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2866 | if (SrcTy->isVectorTy()) // Casting from vector |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2867 | return DestBits == SrcBits; |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2868 | // Casting from something else |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2869 | return false; |
| 2870 | } |
David Blaikie | 9965c5a | 2015-03-23 19:51:23 +0000 | [diff] [blame] | 2871 | if (DestTy->isVectorTy()) // Casting to vector |
| 2872 | return DestBits == SrcBits; |
| 2873 | if (DestTy->isPointerTy()) { // Casting to pointer |
| 2874 | if (SrcTy->isPointerTy()) // Casting from pointer |
| 2875 | return true; |
| 2876 | return SrcTy->isIntegerTy(); // Casting from integral |
| 2877 | } |
| 2878 | if (DestTy->isX86_MMXTy()) { |
| 2879 | if (SrcTy->isVectorTy()) |
| 2880 | return DestBits == SrcBits; // 64-bit vector to MMX |
| 2881 | return false; |
| 2882 | } // Casting to something else |
| 2883 | return false; |
Matt Arsenault | b4019ae | 2013-07-30 22:02:14 +0000 | [diff] [blame] | 2884 | } |
| 2885 | |
Matt Arsenault | cacbb23 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 2886 | bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) { |
| 2887 | if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) |
| 2888 | return false; |
| 2889 | |
| 2890 | if (SrcTy == DestTy) |
| 2891 | return true; |
| 2892 | |
| 2893 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) { |
| 2894 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) { |
| 2895 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 2896 | // An element by element cast. Valid if casting the elements is valid. |
| 2897 | SrcTy = SrcVecTy->getElementType(); |
| 2898 | DestTy = DestVecTy->getElementType(); |
| 2899 | } |
| 2900 | } |
| 2901 | } |
| 2902 | |
| 2903 | if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) { |
| 2904 | if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) { |
| 2905 | return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace(); |
| 2906 | } |
| 2907 | } |
| 2908 | |
| 2909 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2910 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2911 | |
| 2912 | // Could still have vectors of pointers if the number of elements doesn't |
| 2913 | // match |
| 2914 | if (SrcBits == 0 || DestBits == 0) |
| 2915 | return false; |
| 2916 | |
| 2917 | if (SrcBits != DestBits) |
| 2918 | return false; |
| 2919 | |
| 2920 | if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy()) |
| 2921 | return false; |
| 2922 | |
| 2923 | return true; |
| 2924 | } |
| 2925 | |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2926 | bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2927 | const DataLayout &DL) { |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2928 | if (auto *PtrTy = dyn_cast<PointerType>(SrcTy)) |
| 2929 | if (auto *IntTy = dyn_cast<IntegerType>(DestTy)) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2930 | return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy); |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2931 | if (auto *PtrTy = dyn_cast<PointerType>(DestTy)) |
| 2932 | if (auto *IntTy = dyn_cast<IntegerType>(SrcTy)) |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 2933 | return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy); |
Chandler Carruth | 1a3c2c4 | 2014-11-25 08:20:27 +0000 | [diff] [blame] | 2934 | |
| 2935 | return isBitCastable(SrcTy, DestTy); |
| 2936 | } |
| 2937 | |
Matt Arsenault | cacbb23 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 2938 | // Provide a way to get a "cast" where the cast opcode is inferred from the |
| 2939 | // types and size of the operand. This, basically, is a parallel of the |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2940 | // logic in the castIsValid function below. This axiom should hold: |
| 2941 | // castIsValid( getCastOpcode(Val, Ty), Val, Ty) |
| 2942 | // should not assert in castIsValid. In other words, this produces a "correct" |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2943 | // casting opcode for the arguments passed to it. |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2944 | // This routine must be kept in sync with isCastable. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2945 | Instruction::CastOps |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2946 | CastInst::getCastOpcode( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2947 | const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) { |
| 2948 | Type *SrcTy = Src->getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2949 | |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2950 | assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() && |
| 2951 | "Only first class types are castable!"); |
| 2952 | |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2953 | if (SrcTy == DestTy) |
| 2954 | return BitCast; |
| 2955 | |
Matt Arsenault | cacbb23 | 2013-07-30 20:45:05 +0000 | [diff] [blame] | 2956 | // FIXME: Check address space sizes here |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2957 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) |
| 2958 | if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2959 | if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { |
| 2960 | // An element by element cast. Find the appropriate opcode based on the |
| 2961 | // element types. |
| 2962 | SrcTy = SrcVecTy->getElementType(); |
| 2963 | DestTy = DestVecTy->getElementType(); |
| 2964 | } |
| 2965 | |
| 2966 | // Get the bit sizes, we'll need these |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 2967 | unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr |
| 2968 | unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr |
Duncan Sands | a851453 | 2011-05-18 07:13:41 +0000 | [diff] [blame] | 2969 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2970 | // Run through the possibilities ... |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2971 | if (DestTy->isIntegerTy()) { // Casting to integral |
| 2972 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2973 | if (DestBits < SrcBits) |
| 2974 | return Trunc; // int -> smaller int |
| 2975 | else if (DestBits > SrcBits) { // its an extension |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2976 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2977 | return SExt; // signed -> SEXT |
| 2978 | else |
| 2979 | return ZExt; // unsigned -> ZEXT |
| 2980 | } else { |
| 2981 | return BitCast; // Same size, No-op cast |
| 2982 | } |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2983 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2984 | if (DestIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2985 | return FPToSI; // FP -> sint |
| 2986 | else |
| 2987 | return FPToUI; // FP -> uint |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 2988 | } else if (SrcTy->isVectorTy()) { |
| 2989 | assert(DestBits == SrcBits && |
| 2990 | "Casting vector to integer of different width"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2991 | return BitCast; // Same size, no-op cast |
| 2992 | } else { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2993 | assert(SrcTy->isPointerTy() && |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2994 | "Casting from a value that is not first-class type"); |
| 2995 | return PtrToInt; // ptr -> int |
| 2996 | } |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2997 | } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt |
| 2998 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2999 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3000 | return SIToFP; // sint -> FP |
| 3001 | else |
| 3002 | return UIToFP; // uint -> FP |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3003 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3004 | if (DestBits < SrcBits) { |
| 3005 | return FPTrunc; // FP -> smaller FP |
| 3006 | } else if (DestBits > SrcBits) { |
| 3007 | return FPExt; // FP -> larger FP |
| 3008 | } else { |
| 3009 | return BitCast; // same size, no-op cast |
| 3010 | } |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 3011 | } else if (SrcTy->isVectorTy()) { |
| 3012 | assert(DestBits == SrcBits && |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 3013 | "Casting vector to floating point of different width"); |
Devang Patel | e943213 | 2008-11-05 01:37:40 +0000 | [diff] [blame] | 3014 | return BitCast; // same size, no-op cast |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3015 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 3016 | llvm_unreachable("Casting pointer or non-first class to float"); |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 3017 | } else if (DestTy->isVectorTy()) { |
| 3018 | assert(DestBits == SrcBits && |
| 3019 | "Illegal cast to vector (wrong type or size)"); |
| 3020 | return BitCast; |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3021 | } else if (DestTy->isPointerTy()) { |
| 3022 | if (SrcTy->isPointerTy()) { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3023 | if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace()) |
| 3024 | return AddrSpaceCast; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3025 | return BitCast; // ptr -> ptr |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 3026 | } else if (SrcTy->isIntegerTy()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3027 | return IntToPtr; // int -> ptr |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3028 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 3029 | llvm_unreachable("Casting pointer to other than pointer or int"); |
Dale Johannesen | dd224d2 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 3030 | } else if (DestTy->isX86_MMXTy()) { |
Duncan Sands | 27bd0df | 2011-05-18 10:59:25 +0000 | [diff] [blame] | 3031 | if (SrcTy->isVectorTy()) { |
| 3032 | assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX"); |
Dale Johannesen | dd224d2 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 3033 | return BitCast; // 64-bit vector to MMX |
Dale Johannesen | dd224d2 | 2010-09-30 23:57:10 +0000 | [diff] [blame] | 3034 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 3035 | llvm_unreachable("Illegal cast to X86_MMX"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3036 | } |
Ahmed Charles | 636a3d6 | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 3037 | llvm_unreachable("Casting to type that is not first-class"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3038 | } |
| 3039 | |
| 3040 | //===----------------------------------------------------------------------===// |
| 3041 | // CastInst SubClass Constructors |
| 3042 | //===----------------------------------------------------------------------===// |
| 3043 | |
| 3044 | /// Check that the construction parameters for a CastInst are correct. This |
| 3045 | /// could be broken out into the separate constructors but it is useful to have |
| 3046 | /// it in one place and to eliminate the redundant code for getting the sizes |
| 3047 | /// of the types involved. |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3048 | bool |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3049 | CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3050 | |
| 3051 | // Check for type sanity on the arguments |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3052 | Type *SrcTy = S->getType(); |
Evan Cheng | 098d7b7 | 2013-01-10 23:22:53 +0000 | [diff] [blame] | 3053 | |
Chris Lattner | 37bc78a | 2010-01-26 21:51:43 +0000 | [diff] [blame] | 3054 | if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() || |
| 3055 | SrcTy->isAggregateType() || DstTy->isAggregateType()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3056 | return false; |
| 3057 | |
| 3058 | // Get the size of the types in bits, we'll need this later |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 3059 | unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 3060 | unsigned DstBitSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3061 | |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3062 | // If these are vector types, get the lengths of the vectors (using zero for |
| 3063 | // scalar types means that checking that vector lengths match also checks that |
| 3064 | // scalars are not being converted to vectors or vectors to scalars). |
| 3065 | unsigned SrcLength = SrcTy->isVectorTy() ? |
| 3066 | cast<VectorType>(SrcTy)->getNumElements() : 0; |
| 3067 | unsigned DstLength = DstTy->isVectorTy() ? |
| 3068 | cast<VectorType>(DstTy)->getNumElements() : 0; |
| 3069 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3070 | // Switch on the opcode provided |
| 3071 | switch (op) { |
| 3072 | default: return false; // This is an input error |
| 3073 | case Instruction::Trunc: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3074 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 3075 | SrcLength == DstLength && SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3076 | case Instruction::ZExt: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3077 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 3078 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3079 | case Instruction::SExt: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3080 | return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 3081 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3082 | case Instruction::FPTrunc: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3083 | return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 3084 | SrcLength == DstLength && SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3085 | case Instruction::FPExt: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3086 | return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 3087 | SrcLength == DstLength && SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3088 | case Instruction::UIToFP: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3089 | case Instruction::SIToFP: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3090 | return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() && |
| 3091 | SrcLength == DstLength; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3092 | case Instruction::FPToUI: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3093 | case Instruction::FPToSI: |
Duncan Sands | 7f64656 | 2011-05-18 09:21:57 +0000 | [diff] [blame] | 3094 | return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() && |
| 3095 | SrcLength == DstLength; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3096 | case Instruction::PtrToInt: |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 3097 | if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy)) |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 3098 | return false; |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 3099 | if (VectorType *VT = dyn_cast<VectorType>(SrcTy)) |
| 3100 | if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements()) |
| 3101 | return false; |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 3102 | return SrcTy->getScalarType()->isPointerTy() && |
| 3103 | DstTy->getScalarType()->isIntegerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3104 | case Instruction::IntToPtr: |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 3105 | if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy)) |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 3106 | return false; |
Chris Lattner | 8a3df54 | 2012-01-25 01:32:59 +0000 | [diff] [blame] | 3107 | if (VectorType *VT = dyn_cast<VectorType>(SrcTy)) |
| 3108 | if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements()) |
| 3109 | return false; |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 3110 | return SrcTy->getScalarType()->isIntegerTy() && |
| 3111 | DstTy->getScalarType()->isPointerTy(); |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3112 | case Instruction::BitCast: { |
| 3113 | PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType()); |
| 3114 | PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType()); |
| 3115 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3116 | // BitCast implies a no-op cast of type only. No bits change. |
| 3117 | // However, you can't cast pointers to anything but pointers. |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3118 | if (!SrcPtrTy != !DstPtrTy) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3119 | return false; |
| 3120 | |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 3121 | // For non-pointer cases, the cast is okay if the source and destination bit |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3122 | // widths are identical. |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3123 | if (!SrcPtrTy) |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3124 | return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits(); |
| 3125 | |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3126 | // If both are pointers then the address spaces must match. |
| 3127 | if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace()) |
| 3128 | return false; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3129 | |
Matt Arsenault | fc3c91d | 2014-01-22 19:21:33 +0000 | [diff] [blame] | 3130 | // A vector of pointers must have the same number of elements. |
| 3131 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) { |
| 3132 | if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy)) |
| 3133 | return (SrcVecTy->getNumElements() == DstVecTy->getNumElements()); |
| 3134 | |
| 3135 | return false; |
| 3136 | } |
| 3137 | |
| 3138 | return true; |
| 3139 | } |
| 3140 | case Instruction::AddrSpaceCast: { |
| 3141 | PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType()); |
| 3142 | if (!SrcPtrTy) |
| 3143 | return false; |
| 3144 | |
| 3145 | PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType()); |
| 3146 | if (!DstPtrTy) |
| 3147 | return false; |
| 3148 | |
| 3149 | if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace()) |
| 3150 | return false; |
| 3151 | |
| 3152 | if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) { |
| 3153 | if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy)) |
| 3154 | return (SrcVecTy->getNumElements() == DstVecTy->getNumElements()); |
| 3155 | |
| 3156 | return false; |
| 3157 | } |
| 3158 | |
| 3159 | return true; |
| 3160 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3161 | } |
| 3162 | } |
| 3163 | |
| 3164 | TruncInst::TruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3165 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3166 | ) : CastInst(Ty, Trunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3167 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3168 | } |
| 3169 | |
| 3170 | TruncInst::TruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3171 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3172 | ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3173 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3174 | } |
| 3175 | |
| 3176 | ZExtInst::ZExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3177 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3178 | ) : CastInst(Ty, ZExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3179 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
| 3182 | ZExtInst::ZExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3183 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3184 | ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3185 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3186 | } |
| 3187 | SExtInst::SExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3188 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3189 | ) : CastInst(Ty, SExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3190 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3191 | } |
| 3192 | |
Jeff Cohen | cc08c83 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 3193 | SExtInst::SExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3194 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3195 | ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3196 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3197 | } |
| 3198 | |
| 3199 | FPTruncInst::FPTruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3200 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3201 | ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3202 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | FPTruncInst::FPTruncInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3206 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3207 | ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3208 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3209 | } |
| 3210 | |
| 3211 | FPExtInst::FPExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3212 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3213 | ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3214 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
| 3217 | FPExtInst::FPExtInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3218 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3219 | ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3220 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3221 | } |
| 3222 | |
| 3223 | UIToFPInst::UIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3224 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3225 | ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3226 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3227 | } |
| 3228 | |
| 3229 | UIToFPInst::UIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3230 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3231 | ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3232 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3233 | } |
| 3234 | |
| 3235 | SIToFPInst::SIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3236 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3237 | ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3238 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3239 | } |
| 3240 | |
| 3241 | SIToFPInst::SIToFPInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3242 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3243 | ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3244 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
| 3247 | FPToUIInst::FPToUIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3248 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3249 | ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3250 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3251 | } |
| 3252 | |
| 3253 | FPToUIInst::FPToUIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3254 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3255 | ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3256 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
| 3259 | FPToSIInst::FPToSIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3260 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3261 | ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3262 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3263 | } |
| 3264 | |
| 3265 | FPToSIInst::FPToSIInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3266 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3267 | ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3268 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
| 3271 | PtrToIntInst::PtrToIntInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3272 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3273 | ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3274 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3275 | } |
| 3276 | |
| 3277 | PtrToIntInst::PtrToIntInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3278 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3279 | ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3280 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
| 3283 | IntToPtrInst::IntToPtrInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3284 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3285 | ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3286 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3287 | } |
| 3288 | |
| 3289 | IntToPtrInst::IntToPtrInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3290 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3291 | ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3292 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3293 | } |
| 3294 | |
| 3295 | BitCastInst::BitCastInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3296 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3297 | ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3298 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3299 | } |
| 3300 | |
| 3301 | BitCastInst::BitCastInst( |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3302 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3303 | ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 3304 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3305 | } |
Chris Lattner | f16dc00 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 3306 | |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3307 | AddrSpaceCastInst::AddrSpaceCastInst( |
| 3308 | Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore |
| 3309 | ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) { |
| 3310 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast"); |
| 3311 | } |
| 3312 | |
| 3313 | AddrSpaceCastInst::AddrSpaceCastInst( |
| 3314 | Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
| 3315 | ) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) { |
| 3316 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast"); |
| 3317 | } |
| 3318 | |
Chris Lattner | f16dc00 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 3319 | //===----------------------------------------------------------------------===// |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3320 | // CmpInst Classes |
| 3321 | //===----------------------------------------------------------------------===// |
| 3322 | |
Craig Topper | 3186c01 | 2012-09-23 02:12:10 +0000 | [diff] [blame] | 3323 | void CmpInst::anchor() {} |
Chris Lattner | aec33da | 2010-01-22 06:25:37 +0000 | [diff] [blame] | 3324 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3325 | CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 3326 | Value *LHS, Value *RHS, const Twine &Name, |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 3327 | Instruction *InsertBefore) |
Nate Begeman | 66d0a0e | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 3328 | : Instruction(ty, op, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3329 | OperandTraits<CmpInst>::op_begin(this), |
| 3330 | OperandTraits<CmpInst>::operands(this), |
| 3331 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3332 | Op<0>() = LHS; |
| 3333 | Op<1>() = RHS; |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 3334 | setPredicate((Predicate)predicate); |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 3335 | setName(Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3336 | } |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3337 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 3338 | CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 3339 | Value *LHS, Value *RHS, const Twine &Name, |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 3340 | BasicBlock *InsertAtEnd) |
Nate Begeman | 66d0a0e | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 3341 | : Instruction(ty, op, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3342 | OperandTraits<CmpInst>::op_begin(this), |
| 3343 | OperandTraits<CmpInst>::operands(this), |
| 3344 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3345 | Op<0>() = LHS; |
| 3346 | Op<1>() = RHS; |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 3347 | setPredicate((Predicate)predicate); |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 3348 | setName(Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3349 | } |
| 3350 | |
| 3351 | CmpInst * |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3352 | CmpInst::Create(OtherOps Op, unsigned short predicate, |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3353 | Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 3354 | const Twine &Name, Instruction *InsertBefore) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3355 | if (Op == Instruction::ICmp) { |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3356 | if (InsertBefore) |
| 3357 | return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate), |
| 3358 | S1, S2, Name); |
| 3359 | else |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3360 | return new ICmpInst(CmpInst::Predicate(predicate), |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3361 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3362 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3363 | |
| 3364 | if (InsertBefore) |
| 3365 | return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate), |
| 3366 | S1, S2, Name); |
| 3367 | else |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 3368 | return new FCmpInst(CmpInst::Predicate(predicate), |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3369 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3370 | } |
| 3371 | |
| 3372 | CmpInst * |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 3373 | CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 3374 | const Twine &Name, BasicBlock *InsertAtEnd) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3375 | if (Op == Instruction::ICmp) { |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3376 | return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), |
| 3377 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3378 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3379 | return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), |
| 3380 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3381 | } |
| 3382 | |
| 3383 | void CmpInst::swapOperands() { |
| 3384 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 3385 | IC->swapOperands(); |
| 3386 | else |
| 3387 | cast<FCmpInst>(this)->swapOperands(); |
| 3388 | } |
| 3389 | |
Duncan Sands | 95c4ecc | 2011-01-04 12:52:29 +0000 | [diff] [blame] | 3390 | bool CmpInst::isCommutative() const { |
| 3391 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3392 | return IC->isCommutative(); |
| 3393 | return cast<FCmpInst>(this)->isCommutative(); |
| 3394 | } |
| 3395 | |
Duncan Sands | 95c4ecc | 2011-01-04 12:52:29 +0000 | [diff] [blame] | 3396 | bool CmpInst::isEquality() const { |
| 3397 | if (const ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3398 | return IC->isEquality(); |
| 3399 | return cast<FCmpInst>(this)->isEquality(); |
| 3400 | } |
| 3401 | |
| 3402 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3403 | CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3404 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3405 | default: llvm_unreachable("Unknown cmp predicate!"); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3406 | case ICMP_EQ: return ICMP_NE; |
| 3407 | case ICMP_NE: return ICMP_EQ; |
| 3408 | case ICMP_UGT: return ICMP_ULE; |
| 3409 | case ICMP_ULT: return ICMP_UGE; |
| 3410 | case ICMP_UGE: return ICMP_ULT; |
| 3411 | case ICMP_ULE: return ICMP_UGT; |
| 3412 | case ICMP_SGT: return ICMP_SLE; |
| 3413 | case ICMP_SLT: return ICMP_SGE; |
| 3414 | case ICMP_SGE: return ICMP_SLT; |
| 3415 | case ICMP_SLE: return ICMP_SGT; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3416 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3417 | case FCMP_OEQ: return FCMP_UNE; |
| 3418 | case FCMP_ONE: return FCMP_UEQ; |
| 3419 | case FCMP_OGT: return FCMP_ULE; |
| 3420 | case FCMP_OLT: return FCMP_UGE; |
| 3421 | case FCMP_OGE: return FCMP_ULT; |
| 3422 | case FCMP_OLE: return FCMP_UGT; |
| 3423 | case FCMP_UEQ: return FCMP_ONE; |
| 3424 | case FCMP_UNE: return FCMP_OEQ; |
| 3425 | case FCMP_UGT: return FCMP_OLE; |
| 3426 | case FCMP_ULT: return FCMP_OGE; |
| 3427 | case FCMP_UGE: return FCMP_OLT; |
| 3428 | case FCMP_ULE: return FCMP_OGT; |
| 3429 | case FCMP_ORD: return FCMP_UNO; |
| 3430 | case FCMP_UNO: return FCMP_ORD; |
| 3431 | case FCMP_TRUE: return FCMP_FALSE; |
| 3432 | case FCMP_FALSE: return FCMP_TRUE; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3433 | } |
| 3434 | } |
| 3435 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3436 | ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { |
| 3437 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3438 | default: llvm_unreachable("Unknown icmp predicate!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3439 | case ICMP_EQ: case ICMP_NE: |
| 3440 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: |
| 3441 | return pred; |
| 3442 | case ICMP_UGT: return ICMP_SGT; |
| 3443 | case ICMP_ULT: return ICMP_SLT; |
| 3444 | case ICMP_UGE: return ICMP_SGE; |
| 3445 | case ICMP_ULE: return ICMP_SLE; |
| 3446 | } |
| 3447 | } |
| 3448 | |
Nick Lewycky | 8ea81e8 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 3449 | ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) { |
| 3450 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3451 | default: llvm_unreachable("Unknown icmp predicate!"); |
Nick Lewycky | 8ea81e8 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 3452 | case ICMP_EQ: case ICMP_NE: |
| 3453 | case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE: |
| 3454 | return pred; |
| 3455 | case ICMP_SGT: return ICMP_UGT; |
| 3456 | case ICMP_SLT: return ICMP_ULT; |
| 3457 | case ICMP_SGE: return ICMP_UGE; |
| 3458 | case ICMP_SLE: return ICMP_ULE; |
| 3459 | } |
| 3460 | } |
| 3461 | |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3462 | /// Initialize a set of values that all satisfy the condition with C. |
| 3463 | /// |
| 3464 | ConstantRange |
| 3465 | ICmpInst::makeConstantRange(Predicate pred, const APInt &C) { |
| 3466 | APInt Lower(C); |
| 3467 | APInt Upper(C); |
| 3468 | uint32_t BitWidth = C.getBitWidth(); |
| 3469 | switch (pred) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 3470 | default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!"); |
Jakub Staszak | 773be0c | 2013-03-20 23:56:19 +0000 | [diff] [blame] | 3471 | case ICmpInst::ICMP_EQ: ++Upper; break; |
| 3472 | case ICmpInst::ICMP_NE: ++Lower; break; |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 3473 | case ICmpInst::ICMP_ULT: |
| 3474 | Lower = APInt::getMinValue(BitWidth); |
| 3475 | // Check for an empty-set condition. |
| 3476 | if (Lower == Upper) |
| 3477 | return ConstantRange(BitWidth, /*isFullSet=*/false); |
| 3478 | break; |
| 3479 | case ICmpInst::ICMP_SLT: |
| 3480 | Lower = APInt::getSignedMinValue(BitWidth); |
| 3481 | // Check for an empty-set condition. |
| 3482 | if (Lower == Upper) |
| 3483 | return ConstantRange(BitWidth, /*isFullSet=*/false); |
| 3484 | break; |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3485 | case ICmpInst::ICMP_UGT: |
Jakub Staszak | 773be0c | 2013-03-20 23:56:19 +0000 | [diff] [blame] | 3486 | ++Lower; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max) |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 3487 | // Check for an empty-set condition. |
| 3488 | if (Lower == Upper) |
| 3489 | return ConstantRange(BitWidth, /*isFullSet=*/false); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3490 | break; |
| 3491 | case ICmpInst::ICMP_SGT: |
Jakub Staszak | 773be0c | 2013-03-20 23:56:19 +0000 | [diff] [blame] | 3492 | ++Lower; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max) |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 3493 | // Check for an empty-set condition. |
| 3494 | if (Lower == Upper) |
| 3495 | return ConstantRange(BitWidth, /*isFullSet=*/false); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3496 | break; |
| 3497 | case ICmpInst::ICMP_ULE: |
Jakub Staszak | 773be0c | 2013-03-20 23:56:19 +0000 | [diff] [blame] | 3498 | Lower = APInt::getMinValue(BitWidth); ++Upper; |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 3499 | // Check for a full-set condition. |
| 3500 | if (Lower == Upper) |
| 3501 | return ConstantRange(BitWidth, /*isFullSet=*/true); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3502 | break; |
| 3503 | case ICmpInst::ICMP_SLE: |
Jakub Staszak | 773be0c | 2013-03-20 23:56:19 +0000 | [diff] [blame] | 3504 | Lower = APInt::getSignedMinValue(BitWidth); ++Upper; |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 3505 | // Check for a full-set condition. |
| 3506 | if (Lower == Upper) |
| 3507 | return ConstantRange(BitWidth, /*isFullSet=*/true); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3508 | break; |
| 3509 | case ICmpInst::ICMP_UGE: |
| 3510 | Upper = APInt::getMinValue(BitWidth); // Min = Next(Max) |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 3511 | // Check for a full-set condition. |
| 3512 | if (Lower == Upper) |
| 3513 | return ConstantRange(BitWidth, /*isFullSet=*/true); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3514 | break; |
| 3515 | case ICmpInst::ICMP_SGE: |
| 3516 | Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max) |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 3517 | // Check for a full-set condition. |
| 3518 | if (Lower == Upper) |
| 3519 | return ConstantRange(BitWidth, /*isFullSet=*/true); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 3520 | break; |
| 3521 | } |
| 3522 | return ConstantRange(Lower, Upper); |
| 3523 | } |
| 3524 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3525 | CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3526 | switch (pred) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 3527 | default: llvm_unreachable("Unknown cmp predicate!"); |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 3528 | case ICMP_EQ: case ICMP_NE: |
| 3529 | return pred; |
| 3530 | case ICMP_SGT: return ICMP_SLT; |
| 3531 | case ICMP_SLT: return ICMP_SGT; |
| 3532 | case ICMP_SGE: return ICMP_SLE; |
| 3533 | case ICMP_SLE: return ICMP_SGE; |
| 3534 | case ICMP_UGT: return ICMP_ULT; |
| 3535 | case ICMP_ULT: return ICMP_UGT; |
| 3536 | case ICMP_UGE: return ICMP_ULE; |
| 3537 | case ICMP_ULE: return ICMP_UGE; |
| 3538 | |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3539 | case FCMP_FALSE: case FCMP_TRUE: |
| 3540 | case FCMP_OEQ: case FCMP_ONE: |
| 3541 | case FCMP_UEQ: case FCMP_UNE: |
| 3542 | case FCMP_ORD: case FCMP_UNO: |
| 3543 | return pred; |
| 3544 | case FCMP_OGT: return FCMP_OLT; |
| 3545 | case FCMP_OLT: return FCMP_OGT; |
| 3546 | case FCMP_OGE: return FCMP_OLE; |
| 3547 | case FCMP_OLE: return FCMP_OGE; |
| 3548 | case FCMP_UGT: return FCMP_ULT; |
| 3549 | case FCMP_ULT: return FCMP_UGT; |
| 3550 | case FCMP_UGE: return FCMP_ULE; |
| 3551 | case FCMP_ULE: return FCMP_UGE; |
| 3552 | } |
| 3553 | } |
| 3554 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3555 | bool CmpInst::isUnsigned(unsigned short predicate) { |
| 3556 | switch (predicate) { |
| 3557 | default: return false; |
| 3558 | case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: |
| 3559 | case ICmpInst::ICMP_UGE: return true; |
| 3560 | } |
| 3561 | } |
| 3562 | |
Nick Lewycky | 7494b3b | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 3563 | bool CmpInst::isSigned(unsigned short predicate) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 3564 | switch (predicate) { |
| 3565 | default: return false; |
| 3566 | case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: |
| 3567 | case ICmpInst::ICMP_SGE: return true; |
| 3568 | } |
| 3569 | } |
| 3570 | |
| 3571 | bool CmpInst::isOrdered(unsigned short predicate) { |
| 3572 | switch (predicate) { |
| 3573 | default: return false; |
| 3574 | case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: |
| 3575 | case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: |
| 3576 | case FCmpInst::FCMP_ORD: return true; |
| 3577 | } |
| 3578 | } |
| 3579 | |
| 3580 | bool CmpInst::isUnordered(unsigned short predicate) { |
| 3581 | switch (predicate) { |
| 3582 | default: return false; |
| 3583 | case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: |
| 3584 | case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: |
| 3585 | case FCmpInst::FCMP_UNO: return true; |
| 3586 | } |
| 3587 | } |
| 3588 | |
Nick Lewycky | 7494b3b | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 3589 | bool CmpInst::isTrueWhenEqual(unsigned short predicate) { |
| 3590 | switch(predicate) { |
| 3591 | default: return false; |
| 3592 | case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE: |
| 3593 | case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true; |
| 3594 | } |
| 3595 | } |
| 3596 | |
| 3597 | bool CmpInst::isFalseWhenEqual(unsigned short predicate) { |
| 3598 | switch(predicate) { |
| 3599 | case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT: |
| 3600 | case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true; |
| 3601 | default: return false; |
| 3602 | } |
| 3603 | } |
| 3604 | |
| 3605 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3606 | //===----------------------------------------------------------------------===// |
| 3607 | // SwitchInst Implementation |
| 3608 | //===----------------------------------------------------------------------===// |
| 3609 | |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3610 | void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) { |
| 3611 | assert(Value && Default && NumReserved); |
| 3612 | ReservedSpace = NumReserved; |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3613 | setNumHungOffUseOperands(2); |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 3614 | allocHungoffUses(ReservedSpace); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3615 | |
Pete Cooper | eb31b68 | 2015-05-21 22:48:54 +0000 | [diff] [blame] | 3616 | Op<0>() = Value; |
| 3617 | Op<1>() = Default; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3618 | } |
| 3619 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3620 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 3621 | /// switch on and a default destination. The number of additional cases can |
| 3622 | /// be specified here to make memory allocation more efficient. This |
| 3623 | /// constructor can also autoinsert before another instruction. |
| 3624 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 3625 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3626 | : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch, |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3627 | nullptr, 0, InsertBefore) { |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3628 | init(Value, Default, 2+NumCases*2); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3629 | } |
| 3630 | |
| 3631 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 3632 | /// switch on and a default destination. The number of additional cases can |
| 3633 | /// be specified here to make memory allocation more efficient. This |
| 3634 | /// constructor also autoinserts at the end of the specified BasicBlock. |
| 3635 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 3636 | BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 3637 | : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch, |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3638 | nullptr, 0, InsertAtEnd) { |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3639 | init(Value, Default, 2+NumCases*2); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 3640 | } |
| 3641 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 3642 | SwitchInst::SwitchInst(const SwitchInst &SI) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3643 | : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) { |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3644 | init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands()); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3645 | setNumHungOffUseOperands(SI.getNumOperands()); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3646 | Use *OL = getOperandList(); |
| 3647 | const Use *InOL = SI.getOperandList(); |
Chris Lattner | baf0015 | 2010-11-17 05:41:46 +0000 | [diff] [blame] | 3648 | for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3649 | OL[i] = InOL[i]; |
| 3650 | OL[i+1] = InOL[i+1]; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3651 | } |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 3652 | SubclassOptionalData = SI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3653 | } |
| 3654 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3655 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3656 | /// addCase - Add an entry to the switch instruction... |
| 3657 | /// |
Chris Lattner | 47ac187 | 2005-02-24 05:32:09 +0000 | [diff] [blame] | 3658 | void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3659 | unsigned NewCaseIdx = getNumCases(); |
| 3660 | unsigned OpNo = getNumOperands(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3661 | if (OpNo+2 > ReservedSpace) |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3662 | growOperands(); // Get more space! |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3663 | // Initialize some new operands. |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 3664 | assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3665 | setNumHungOffUseOperands(OpNo+2); |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 3666 | CaseIt Case(this, NewCaseIdx); |
| 3667 | Case.setValue(OnVal); |
Stepan Dyatkovskiy | 5b648af | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 3668 | Case.setSuccessor(Dest); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3669 | } |
| 3670 | |
Stepan Dyatkovskiy | 513aaa5 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3671 | /// removeCase - This method removes the specified case and its successor |
| 3672 | /// from the switch instruction. |
Bob Wilson | e407736 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 3673 | void SwitchInst::removeCase(CaseIt i) { |
Stepan Dyatkovskiy | 5b648af | 2012-03-08 07:06:20 +0000 | [diff] [blame] | 3674 | unsigned idx = i.getCaseIndex(); |
| 3675 | |
Stepan Dyatkovskiy | 513aaa5 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3676 | assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3677 | |
| 3678 | unsigned NumOps = getNumOperands(); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3679 | Use *OL = getOperandList(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3680 | |
Jay Foad | 1427772 | 2011-02-01 09:22:34 +0000 | [diff] [blame] | 3681 | // Overwrite this case with the end of the list. |
Stepan Dyatkovskiy | 513aaa5 | 2012-02-01 07:49:51 +0000 | [diff] [blame] | 3682 | if (2 + (idx + 1) * 2 != NumOps) { |
| 3683 | OL[2 + idx * 2] = OL[NumOps - 2]; |
| 3684 | OL[2 + idx * 2 + 1] = OL[NumOps - 1]; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3685 | } |
| 3686 | |
| 3687 | // Nuke the last value. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3688 | OL[NumOps-2].set(nullptr); |
| 3689 | OL[NumOps-2+1].set(nullptr); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3690 | setNumHungOffUseOperands(NumOps-2); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3691 | } |
| 3692 | |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3693 | /// growOperands - grow operands - This grows the operand list in response |
| 3694 | /// to a push_back style of operation. This grows the number of ops by 3 times. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3695 | /// |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3696 | void SwitchInst::growOperands() { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3697 | unsigned e = getNumOperands(); |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3698 | unsigned NumOps = e*3; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3699 | |
| 3700 | ReservedSpace = NumOps; |
Pete Cooper | 93f9ff5 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 3701 | growHungoffUses(ReservedSpace); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3702 | } |
| 3703 | |
| 3704 | |
| 3705 | BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const { |
| 3706 | return getSuccessor(idx); |
| 3707 | } |
| 3708 | unsigned SwitchInst::getNumSuccessorsV() const { |
| 3709 | return getNumSuccessors(); |
| 3710 | } |
| 3711 | void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 3712 | setSuccessor(idx, B); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3713 | } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3714 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3715 | //===----------------------------------------------------------------------===// |
Jay Foad | bbb91f2 | 2011-01-16 15:30:52 +0000 | [diff] [blame] | 3716 | // IndirectBrInst Implementation |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3717 | //===----------------------------------------------------------------------===// |
| 3718 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3719 | void IndirectBrInst::init(Value *Address, unsigned NumDests) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3720 | assert(Address && Address->getType()->isPointerTy() && |
Chris Lattner | 6747b4c | 2009-10-29 05:53:32 +0000 | [diff] [blame] | 3721 | "Address of indirectbr must be a pointer"); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3722 | ReservedSpace = 1+NumDests; |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3723 | setNumHungOffUseOperands(1); |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 3724 | allocHungoffUses(ReservedSpace); |
| 3725 | |
Pete Cooper | eb31b68 | 2015-05-21 22:48:54 +0000 | [diff] [blame] | 3726 | Op<0>() = Address; |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3727 | } |
| 3728 | |
| 3729 | |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3730 | /// growOperands - grow operands - This grows the operand list in response |
| 3731 | /// to a push_back style of operation. This grows the number of ops by 2 times. |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3732 | /// |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3733 | void IndirectBrInst::growOperands() { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3734 | unsigned e = getNumOperands(); |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3735 | unsigned NumOps = e*2; |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3736 | |
| 3737 | ReservedSpace = NumOps; |
Pete Cooper | 93f9ff5 | 2015-06-10 22:38:41 +0000 | [diff] [blame] | 3738 | growHungoffUses(ReservedSpace); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3739 | } |
| 3740 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3741 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, |
| 3742 | Instruction *InsertBefore) |
| 3743 | : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr, |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3744 | nullptr, 0, InsertBefore) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3745 | init(Address, NumCases); |
| 3746 | } |
| 3747 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3748 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, |
| 3749 | BasicBlock *InsertAtEnd) |
| 3750 | : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr, |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3751 | nullptr, 0, InsertAtEnd) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3752 | init(Address, NumCases); |
| 3753 | } |
| 3754 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3755 | IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI) |
Pete Cooper | 3fc3040 | 2015-06-10 22:38:46 +0000 | [diff] [blame] | 3756 | : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr, |
| 3757 | nullptr, IBI.getNumOperands()) { |
| 3758 | allocHungoffUses(IBI.getNumOperands()); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3759 | Use *OL = getOperandList(); |
| 3760 | const Use *InOL = IBI.getOperandList(); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3761 | for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i) |
| 3762 | OL[i] = InOL[i]; |
| 3763 | SubclassOptionalData = IBI.SubclassOptionalData; |
| 3764 | } |
| 3765 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3766 | /// addDestination - Add a destination. |
| 3767 | /// |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3768 | void IndirectBrInst::addDestination(BasicBlock *DestBB) { |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3769 | unsigned OpNo = getNumOperands(); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3770 | if (OpNo+1 > ReservedSpace) |
Jay Foad | e98f29d | 2011-04-01 08:00:58 +0000 | [diff] [blame] | 3771 | growOperands(); // Get more space! |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3772 | // Initialize some new operands. |
| 3773 | assert(OpNo < ReservedSpace && "Growing didn't work!"); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3774 | setNumHungOffUseOperands(OpNo+1); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3775 | getOperandList()[OpNo] = DestBB; |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3776 | } |
| 3777 | |
| 3778 | /// removeDestination - This method removes the specified successor from the |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3779 | /// indirectbr instruction. |
| 3780 | void IndirectBrInst::removeDestination(unsigned idx) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3781 | assert(idx < getNumOperands()-1 && "Successor index out of range!"); |
| 3782 | |
| 3783 | unsigned NumOps = getNumOperands(); |
Pete Cooper | 74510a4 | 2015-06-12 17:48:05 +0000 | [diff] [blame] | 3784 | Use *OL = getOperandList(); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3785 | |
| 3786 | // Replace this value with the last one. |
| 3787 | OL[idx+1] = OL[NumOps-1]; |
| 3788 | |
| 3789 | // Nuke the last value. |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 3790 | OL[NumOps-1].set(nullptr); |
Pete Cooper | b4eede2 | 2015-06-12 17:48:10 +0000 | [diff] [blame] | 3791 | setNumHungOffUseOperands(NumOps-1); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3792 | } |
| 3793 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3794 | BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3795 | return getSuccessor(idx); |
| 3796 | } |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3797 | unsigned IndirectBrInst::getNumSuccessorsV() const { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3798 | return getNumSuccessors(); |
| 3799 | } |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3800 | void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3801 | setSuccessor(idx, B); |
| 3802 | } |
| 3803 | |
| 3804 | //===----------------------------------------------------------------------===// |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3805 | // cloneImpl() implementations |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3806 | //===----------------------------------------------------------------------===// |
| 3807 | |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3808 | // Define these methods here so vtables don't get emitted into every translation |
| 3809 | // unit that uses these classes. |
| 3810 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3811 | GetElementPtrInst *GetElementPtrInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3812 | return new (getNumOperands()) GetElementPtrInst(*this); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3813 | } |
| 3814 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3815 | BinaryOperator *BinaryOperator::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3816 | return Create(getOpcode(), Op<0>(), Op<1>()); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3817 | } |
| 3818 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3819 | FCmpInst *FCmpInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3820 | return new FCmpInst(getPredicate(), Op<0>(), Op<1>()); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3821 | } |
| 3822 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3823 | ICmpInst *ICmpInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3824 | return new ICmpInst(getPredicate(), Op<0>(), Op<1>()); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 3825 | } |
| 3826 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3827 | ExtractValueInst *ExtractValueInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3828 | return new ExtractValueInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3829 | } |
| 3830 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3831 | InsertValueInst *InsertValueInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3832 | return new InsertValueInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3833 | } |
| 3834 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3835 | AllocaInst *AllocaInst::cloneImpl() const { |
David Majnemer | 6b3244c | 2014-04-30 16:12:21 +0000 | [diff] [blame] | 3836 | AllocaInst *Result = new AllocaInst(getAllocatedType(), |
| 3837 | (Value *)getOperand(0), getAlignment()); |
| 3838 | Result->setUsedWithInAlloca(isUsedWithInAlloca()); |
| 3839 | return Result; |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3840 | } |
| 3841 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3842 | LoadInst *LoadInst::cloneImpl() const { |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 3843 | return new LoadInst(getOperand(0), Twine(), isVolatile(), |
| 3844 | getAlignment(), getOrdering(), getSynchScope()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3845 | } |
| 3846 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3847 | StoreInst *StoreInst::cloneImpl() const { |
Eli Friedman | cad9f2a | 2011-08-10 17:39:11 +0000 | [diff] [blame] | 3848 | return new StoreInst(getOperand(0), getOperand(1), isVolatile(), |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 3849 | getAlignment(), getOrdering(), getSynchScope()); |
| 3850 | |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3851 | } |
| 3852 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3853 | AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const { |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3854 | AtomicCmpXchgInst *Result = |
| 3855 | new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2), |
Tim Northover | e94a518 | 2014-03-11 10:48:52 +0000 | [diff] [blame] | 3856 | getSuccessOrdering(), getFailureOrdering(), |
| 3857 | getSynchScope()); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3858 | Result->setVolatile(isVolatile()); |
Tim Northover | 420a216 | 2014-06-13 14:24:07 +0000 | [diff] [blame] | 3859 | Result->setWeak(isWeak()); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3860 | return Result; |
| 3861 | } |
| 3862 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3863 | AtomicRMWInst *AtomicRMWInst::cloneImpl() const { |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 3864 | AtomicRMWInst *Result = |
| 3865 | new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1), |
| 3866 | getOrdering(), getSynchScope()); |
| 3867 | Result->setVolatile(isVolatile()); |
| 3868 | return Result; |
| 3869 | } |
| 3870 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3871 | FenceInst *FenceInst::cloneImpl() const { |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 3872 | return new FenceInst(getContext(), getOrdering(), getSynchScope()); |
| 3873 | } |
| 3874 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3875 | TruncInst *TruncInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3876 | return new TruncInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3877 | } |
| 3878 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3879 | ZExtInst *ZExtInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3880 | return new ZExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3881 | } |
| 3882 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3883 | SExtInst *SExtInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3884 | return new SExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3885 | } |
| 3886 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3887 | FPTruncInst *FPTruncInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3888 | return new FPTruncInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3889 | } |
| 3890 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3891 | FPExtInst *FPExtInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3892 | return new FPExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3893 | } |
| 3894 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3895 | UIToFPInst *UIToFPInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3896 | return new UIToFPInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3897 | } |
| 3898 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3899 | SIToFPInst *SIToFPInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3900 | return new SIToFPInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3901 | } |
| 3902 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3903 | FPToUIInst *FPToUIInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3904 | return new FPToUIInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3905 | } |
| 3906 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3907 | FPToSIInst *FPToSIInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3908 | return new FPToSIInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3911 | PtrToIntInst *PtrToIntInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3912 | return new PtrToIntInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3913 | } |
| 3914 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3915 | IntToPtrInst *IntToPtrInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3916 | return new IntToPtrInst(getOperand(0), getType()); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3917 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3918 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3919 | BitCastInst *BitCastInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3920 | return new BitCastInst(getOperand(0), getType()); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3921 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3922 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3923 | AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const { |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 3924 | return new AddrSpaceCastInst(getOperand(0), getType()); |
| 3925 | } |
| 3926 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3927 | CallInst *CallInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3928 | return new(getNumOperands()) CallInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3929 | } |
| 3930 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3931 | SelectInst *SelectInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3932 | return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2)); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3933 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3934 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3935 | VAArgInst *VAArgInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3936 | return new VAArgInst(getOperand(0), getType()); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3937 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3938 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3939 | ExtractElementInst *ExtractElementInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3940 | return ExtractElementInst::Create(getOperand(0), getOperand(1)); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3941 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3942 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3943 | InsertElementInst *InsertElementInst::cloneImpl() const { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 3944 | return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2)); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3945 | } |
| 3946 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3947 | ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const { |
Chris Lattner | 1dcb654 | 2012-01-25 23:49:49 +0000 | [diff] [blame] | 3948 | return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2)); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3949 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3950 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3951 | PHINode *PHINode::cloneImpl() const { return new PHINode(*this); } |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3952 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3953 | LandingPadInst *LandingPadInst::cloneImpl() const { |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 3954 | return new LandingPadInst(*this); |
| 3955 | } |
| 3956 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3957 | ReturnInst *ReturnInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3958 | return new(getNumOperands()) ReturnInst(*this); |
| 3959 | } |
| 3960 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3961 | BranchInst *BranchInst::cloneImpl() const { |
Jay Foad | d81f3c9 | 2011-01-07 20:29:02 +0000 | [diff] [blame] | 3962 | return new(getNumOperands()) BranchInst(*this); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3963 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3964 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3965 | SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3966 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3967 | IndirectBrInst *IndirectBrInst::cloneImpl() const { |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3968 | return new IndirectBrInst(*this); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3969 | } |
| 3970 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3971 | InvokeInst *InvokeInst::cloneImpl() const { |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3972 | return new(getNumOperands()) InvokeInst(*this); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3973 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3974 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 3975 | ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); } |
Bill Wendling | f891bf8 | 2011-07-31 06:30:59 +0000 | [diff] [blame] | 3976 | |
Joseph Tremoulet | 9ce71f7 | 2015-09-03 09:09:43 +0000 | [diff] [blame] | 3977 | CleanupEndPadInst *CleanupEndPadInst::cloneImpl() const { |
| 3978 | return new (getNumOperands()) CleanupEndPadInst(*this); |
| 3979 | } |
| 3980 | |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3981 | CleanupReturnInst *CleanupReturnInst::cloneImpl() const { |
| 3982 | return new (getNumOperands()) CleanupReturnInst(*this); |
| 3983 | } |
| 3984 | |
| 3985 | CatchEndPadInst *CatchEndPadInst::cloneImpl() const { |
| 3986 | return new (getNumOperands()) CatchEndPadInst(*this); |
| 3987 | } |
| 3988 | |
| 3989 | CatchReturnInst *CatchReturnInst::cloneImpl() const { |
David Majnemer | 0bc0eef | 2015-08-15 02:46:08 +0000 | [diff] [blame] | 3990 | return new (getNumOperands()) CatchReturnInst(*this); |
David Majnemer | 654e130 | 2015-07-31 17:58:14 +0000 | [diff] [blame] | 3991 | } |
| 3992 | |
| 3993 | CatchPadInst *CatchPadInst::cloneImpl() const { |
| 3994 | return new (getNumOperands()) CatchPadInst(*this); |
| 3995 | } |
| 3996 | |
| 3997 | TerminatePadInst *TerminatePadInst::cloneImpl() const { |
| 3998 | return new (getNumOperands()) TerminatePadInst(*this); |
| 3999 | } |
| 4000 | |
| 4001 | CleanupPadInst *CleanupPadInst::cloneImpl() const { |
| 4002 | return new (getNumOperands()) CleanupPadInst(*this); |
| 4003 | } |
| 4004 | |
Pete Cooper | 75403d7 | 2015-06-24 20:22:23 +0000 | [diff] [blame] | 4005 | UnreachableInst *UnreachableInst::cloneImpl() const { |
Nick Lewycky | 42fb745 | 2009-09-27 07:38:41 +0000 | [diff] [blame] | 4006 | LLVMContext &Context = getContext(); |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 4007 | return new UnreachableInst(Context); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 4008 | } |