Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1 | //===-- Execution.cpp - Implement code to simulate the program ------------===// |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 10 | // This file contains the actual instruction interpreter. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Interpreter.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/APInt.h" |
| 16 | #include "llvm/ADT/Statistic.h" |
| 17 | #include "llvm/CodeGen/IntrinsicLowering.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Constants.h" |
| 19 | #include "llvm/IR/DerivedTypes.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 20 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Instructions.h" |
Chris Lattner | 663ceeb | 2008-07-08 17:25:49 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MathExtras.h" |
Gabor Greif | 724441e | 2007-10-11 19:40:35 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 27 | #include <cmath> |
Chris Lattner | 4af6de8 | 2003-11-25 20:44:56 +0000 | [diff] [blame] | 28 | using namespace llvm; |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 29 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 30 | #define DEBUG_TYPE "interpreter" |
| 31 | |
Chris Lattner | cecf56b | 2006-12-19 22:56:53 +0000 | [diff] [blame] | 32 | STATISTIC(NumDynamicInsts, "Number of dynamic instructions executed"); |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 663ceeb | 2008-07-08 17:25:49 +0000 | [diff] [blame] | 34 | static cl::opt<bool> PrintVolatile("interpreter-print-volatile", cl::Hidden, |
| 35 | cl::desc("make the interpreter print every volatile load and store")); |
| 36 | |
Chris Lattner | 2e42d3a | 2001-10-15 05:51:48 +0000 | [diff] [blame] | 37 | //===----------------------------------------------------------------------===// |
Reid Spencer | e1aa066 | 2007-03-03 06:22:22 +0000 | [diff] [blame] | 38 | // Various Helper Functions |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 41 | static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF) { |
Brian Gaeke | 03e43dc | 2003-10-24 19:59:01 +0000 | [diff] [blame] | 42 | SF.Values[V] = Val; |
Chris Lattner | 39bb5b4 | 2001-10-15 13:25:40 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Chris Lattner | 2adcd83 | 2002-05-03 19:52:30 +0000 | [diff] [blame] | 45 | //===----------------------------------------------------------------------===// |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 46 | // Binary Instruction Implementations |
| 47 | //===----------------------------------------------------------------------===// |
| 48 | |
| 49 | #define IMPLEMENT_BINARY_OPERATOR(OP, TY) \ |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 50 | case Type::TY##TyID: \ |
| 51 | Dest.TY##Val = Src1.TY##Val OP Src2.TY##Val; \ |
| 52 | break |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 53 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 54 | static void executeFAddInst(GenericValue &Dest, GenericValue Src1, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 55 | GenericValue Src2, Type *Ty) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 56 | switch (Ty->getTypeID()) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 57 | IMPLEMENT_BINARY_OPERATOR(+, Float); |
| 58 | IMPLEMENT_BINARY_OPERATOR(+, Double); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 59 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 60 | dbgs() << "Unhandled type for FAdd instruction: " << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 61 | llvm_unreachable(nullptr); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 62 | } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 65 | static void executeFSubInst(GenericValue &Dest, GenericValue Src1, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 66 | GenericValue Src2, Type *Ty) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 67 | switch (Ty->getTypeID()) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 68 | IMPLEMENT_BINARY_OPERATOR(-, Float); |
| 69 | IMPLEMENT_BINARY_OPERATOR(-, Double); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 70 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 71 | dbgs() << "Unhandled type for FSub instruction: " << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 72 | llvm_unreachable(nullptr); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 73 | } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 76 | static void executeFMulInst(GenericValue &Dest, GenericValue Src1, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 77 | GenericValue Src2, Type *Ty) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 78 | switch (Ty->getTypeID()) { |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 79 | IMPLEMENT_BINARY_OPERATOR(*, Float); |
| 80 | IMPLEMENT_BINARY_OPERATOR(*, Double); |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 81 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 82 | dbgs() << "Unhandled type for FMul instruction: " << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 83 | llvm_unreachable(nullptr); |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 84 | } |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Reid Spencer | e1aa066 | 2007-03-03 06:22:22 +0000 | [diff] [blame] | 87 | static void executeFDivInst(GenericValue &Dest, GenericValue Src1, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 88 | GenericValue Src2, Type *Ty) { |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 89 | switch (Ty->getTypeID()) { |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 90 | IMPLEMENT_BINARY_OPERATOR(/, Float); |
| 91 | IMPLEMENT_BINARY_OPERATOR(/, Double); |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 92 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 93 | dbgs() << "Unhandled type for FDiv instruction: " << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 94 | llvm_unreachable(nullptr); |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 95 | } |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Reid Spencer | e1aa066 | 2007-03-03 06:22:22 +0000 | [diff] [blame] | 98 | static void executeFRemInst(GenericValue &Dest, GenericValue Src1, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 99 | GenericValue Src2, Type *Ty) { |
Reid Spencer | 0a783f7 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 100 | switch (Ty->getTypeID()) { |
Chris Lattner | bb76f02 | 2001-10-30 20:27:31 +0000 | [diff] [blame] | 101 | case Type::FloatTyID: |
| 102 | Dest.FloatVal = fmod(Src1.FloatVal, Src2.FloatVal); |
| 103 | break; |
| 104 | case Type::DoubleTyID: |
| 105 | Dest.DoubleVal = fmod(Src1.DoubleVal, Src2.DoubleVal); |
| 106 | break; |
| 107 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 108 | dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 109 | llvm_unreachable(nullptr); |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 110 | } |
Chris Lattner | c259316 | 2001-10-27 08:28:11 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 113 | #define IMPLEMENT_INTEGER_ICMP(OP, TY) \ |
| 114 | case Type::IntegerTyID: \ |
| 115 | Dest.IntVal = APInt(1,Src1.IntVal.OP(Src2.IntVal)); \ |
| 116 | break; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 117 | |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 118 | #define IMPLEMENT_VECTOR_INTEGER_ICMP(OP, TY) \ |
| 119 | case Type::VectorTyID: { \ |
| 120 | assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); \ |
| 121 | Dest.AggregateVal.resize( Src1.AggregateVal.size() ); \ |
| 122 | for( uint32_t _i=0;_i<Src1.AggregateVal.size();_i++) \ |
| 123 | Dest.AggregateVal[_i].IntVal = APInt(1, \ |
| 124 | Src1.AggregateVal[_i].IntVal.OP(Src2.AggregateVal[_i].IntVal));\ |
| 125 | } break; |
| 126 | |
Chris Lattner | fd506f5 | 2003-04-23 19:55:35 +0000 | [diff] [blame] | 127 | // Handle pointers specially because they must be compared with only as much |
| 128 | // width as the host has. We _do not_ want to be comparing 64 bit values when |
| 129 | // running on a 32-bit target, otherwise the upper 32 bits might mess up |
| 130 | // comparisons if they contain garbage. |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 131 | #define IMPLEMENT_POINTER_ICMP(OP) \ |
Chris Lattner | fd506f5 | 2003-04-23 19:55:35 +0000 | [diff] [blame] | 132 | case Type::PointerTyID: \ |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 133 | Dest.IntVal = APInt(1,(void*)(intptr_t)Src1.PointerVal OP \ |
| 134 | (void*)(intptr_t)Src2.PointerVal); \ |
| 135 | break; |
Chris Lattner | fd506f5 | 2003-04-23 19:55:35 +0000 | [diff] [blame] | 136 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 137 | static GenericValue executeICMP_EQ(GenericValue Src1, GenericValue Src2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 138 | Type *Ty) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 139 | GenericValue Dest; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 140 | switch (Ty->getTypeID()) { |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 141 | IMPLEMENT_INTEGER_ICMP(eq,Ty); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 142 | IMPLEMENT_VECTOR_INTEGER_ICMP(eq,Ty); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 143 | IMPLEMENT_POINTER_ICMP(==); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 144 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 145 | dbgs() << "Unhandled type for ICMP_EQ predicate: " << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 146 | llvm_unreachable(nullptr); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 147 | } |
| 148 | return Dest; |
| 149 | } |
| 150 | |
| 151 | static GenericValue executeICMP_NE(GenericValue Src1, GenericValue Src2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 152 | Type *Ty) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 153 | GenericValue Dest; |
| 154 | switch (Ty->getTypeID()) { |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 155 | IMPLEMENT_INTEGER_ICMP(ne,Ty); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 156 | IMPLEMENT_VECTOR_INTEGER_ICMP(ne,Ty); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 157 | IMPLEMENT_POINTER_ICMP(!=); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 158 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 159 | dbgs() << "Unhandled type for ICMP_NE predicate: " << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 160 | llvm_unreachable(nullptr); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 161 | } |
| 162 | return Dest; |
| 163 | } |
| 164 | |
| 165 | static GenericValue executeICMP_ULT(GenericValue Src1, GenericValue Src2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 166 | Type *Ty) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 167 | GenericValue Dest; |
| 168 | switch (Ty->getTypeID()) { |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 169 | IMPLEMENT_INTEGER_ICMP(ult,Ty); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 170 | IMPLEMENT_VECTOR_INTEGER_ICMP(ult,Ty); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 171 | IMPLEMENT_POINTER_ICMP(<); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 172 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 173 | dbgs() << "Unhandled type for ICMP_ULT predicate: " << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 174 | llvm_unreachable(nullptr); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 175 | } |
| 176 | return Dest; |
| 177 | } |
| 178 | |
| 179 | static GenericValue executeICMP_SLT(GenericValue Src1, GenericValue Src2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 180 | Type *Ty) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 181 | GenericValue Dest; |
| 182 | switch (Ty->getTypeID()) { |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 183 | IMPLEMENT_INTEGER_ICMP(slt,Ty); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 184 | IMPLEMENT_VECTOR_INTEGER_ICMP(slt,Ty); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 185 | IMPLEMENT_POINTER_ICMP(<); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 186 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 187 | dbgs() << "Unhandled type for ICMP_SLT predicate: " << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 188 | llvm_unreachable(nullptr); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 189 | } |
| 190 | return Dest; |
| 191 | } |
| 192 | |
| 193 | static GenericValue executeICMP_UGT(GenericValue Src1, GenericValue Src2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 194 | Type *Ty) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 195 | GenericValue Dest; |
| 196 | switch (Ty->getTypeID()) { |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 197 | IMPLEMENT_INTEGER_ICMP(ugt,Ty); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 198 | IMPLEMENT_VECTOR_INTEGER_ICMP(ugt,Ty); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 199 | IMPLEMENT_POINTER_ICMP(>); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 200 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 201 | dbgs() << "Unhandled type for ICMP_UGT predicate: " << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 202 | llvm_unreachable(nullptr); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 203 | } |
| 204 | return Dest; |
| 205 | } |
| 206 | |
| 207 | static GenericValue executeICMP_SGT(GenericValue Src1, GenericValue Src2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 208 | Type *Ty) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 209 | GenericValue Dest; |
| 210 | switch (Ty->getTypeID()) { |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 211 | IMPLEMENT_INTEGER_ICMP(sgt,Ty); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 212 | IMPLEMENT_VECTOR_INTEGER_ICMP(sgt,Ty); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 213 | IMPLEMENT_POINTER_ICMP(>); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 214 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 215 | dbgs() << "Unhandled type for ICMP_SGT predicate: " << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 216 | llvm_unreachable(nullptr); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 217 | } |
| 218 | return Dest; |
| 219 | } |
| 220 | |
| 221 | static GenericValue executeICMP_ULE(GenericValue Src1, GenericValue Src2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 222 | Type *Ty) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 223 | GenericValue Dest; |
| 224 | switch (Ty->getTypeID()) { |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 225 | IMPLEMENT_INTEGER_ICMP(ule,Ty); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 226 | IMPLEMENT_VECTOR_INTEGER_ICMP(ule,Ty); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 227 | IMPLEMENT_POINTER_ICMP(<=); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 228 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 229 | dbgs() << "Unhandled type for ICMP_ULE predicate: " << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 230 | llvm_unreachable(nullptr); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 231 | } |
| 232 | return Dest; |
| 233 | } |
| 234 | |
| 235 | static GenericValue executeICMP_SLE(GenericValue Src1, GenericValue Src2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 236 | Type *Ty) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 237 | GenericValue Dest; |
| 238 | switch (Ty->getTypeID()) { |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 239 | IMPLEMENT_INTEGER_ICMP(sle,Ty); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 240 | IMPLEMENT_VECTOR_INTEGER_ICMP(sle,Ty); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 241 | IMPLEMENT_POINTER_ICMP(<=); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 242 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 243 | dbgs() << "Unhandled type for ICMP_SLE predicate: " << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 244 | llvm_unreachable(nullptr); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 245 | } |
| 246 | return Dest; |
| 247 | } |
| 248 | |
| 249 | static GenericValue executeICMP_UGE(GenericValue Src1, GenericValue Src2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 250 | Type *Ty) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 251 | GenericValue Dest; |
| 252 | switch (Ty->getTypeID()) { |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 253 | IMPLEMENT_INTEGER_ICMP(uge,Ty); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 254 | IMPLEMENT_VECTOR_INTEGER_ICMP(uge,Ty); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 255 | IMPLEMENT_POINTER_ICMP(>=); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 256 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 257 | dbgs() << "Unhandled type for ICMP_UGE predicate: " << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 258 | llvm_unreachable(nullptr); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 259 | } |
| 260 | return Dest; |
| 261 | } |
| 262 | |
| 263 | static GenericValue executeICMP_SGE(GenericValue Src1, GenericValue Src2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 264 | Type *Ty) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 265 | GenericValue Dest; |
| 266 | switch (Ty->getTypeID()) { |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 267 | IMPLEMENT_INTEGER_ICMP(sge,Ty); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 268 | IMPLEMENT_VECTOR_INTEGER_ICMP(sge,Ty); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 269 | IMPLEMENT_POINTER_ICMP(>=); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 270 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 271 | dbgs() << "Unhandled type for ICMP_SGE predicate: " << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 272 | llvm_unreachable(nullptr); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 273 | } |
| 274 | return Dest; |
| 275 | } |
| 276 | |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 277 | void Interpreter::visitICmpInst(ICmpInst &I) { |
| 278 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 279 | Type *Ty = I.getOperand(0)->getType(); |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 280 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 281 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
| 282 | GenericValue R; // Result |
| 283 | |
| 284 | switch (I.getPredicate()) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 285 | case ICmpInst::ICMP_EQ: R = executeICMP_EQ(Src1, Src2, Ty); break; |
| 286 | case ICmpInst::ICMP_NE: R = executeICMP_NE(Src1, Src2, Ty); break; |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 287 | case ICmpInst::ICMP_ULT: R = executeICMP_ULT(Src1, Src2, Ty); break; |
| 288 | case ICmpInst::ICMP_SLT: R = executeICMP_SLT(Src1, Src2, Ty); break; |
| 289 | case ICmpInst::ICMP_UGT: R = executeICMP_UGT(Src1, Src2, Ty); break; |
| 290 | case ICmpInst::ICMP_SGT: R = executeICMP_SGT(Src1, Src2, Ty); break; |
| 291 | case ICmpInst::ICMP_ULE: R = executeICMP_ULE(Src1, Src2, Ty); break; |
| 292 | case ICmpInst::ICMP_SLE: R = executeICMP_SLE(Src1, Src2, Ty); break; |
| 293 | case ICmpInst::ICMP_UGE: R = executeICMP_UGE(Src1, Src2, Ty); break; |
| 294 | case ICmpInst::ICMP_SGE: R = executeICMP_SGE(Src1, Src2, Ty); break; |
| 295 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 296 | dbgs() << "Don't know how to handle this ICmp predicate!\n-->" << I; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 297 | llvm_unreachable(nullptr); |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | SetValue(&I, R, SF); |
| 301 | } |
| 302 | |
| 303 | #define IMPLEMENT_FCMP(OP, TY) \ |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 304 | case Type::TY##TyID: \ |
| 305 | Dest.IntVal = APInt(1,Src1.TY##Val OP Src2.TY##Val); \ |
| 306 | break |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 307 | |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 308 | #define IMPLEMENT_VECTOR_FCMP_T(OP, TY) \ |
| 309 | assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); \ |
| 310 | Dest.AggregateVal.resize( Src1.AggregateVal.size() ); \ |
| 311 | for( uint32_t _i=0;_i<Src1.AggregateVal.size();_i++) \ |
| 312 | Dest.AggregateVal[_i].IntVal = APInt(1, \ |
| 313 | Src1.AggregateVal[_i].TY##Val OP Src2.AggregateVal[_i].TY##Val);\ |
| 314 | break; |
| 315 | |
| 316 | #define IMPLEMENT_VECTOR_FCMP(OP) \ |
| 317 | case Type::VectorTyID: \ |
| 318 | if(dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) { \ |
| 319 | IMPLEMENT_VECTOR_FCMP_T(OP, Float); \ |
| 320 | } else { \ |
| 321 | IMPLEMENT_VECTOR_FCMP_T(OP, Double); \ |
| 322 | } |
| 323 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 324 | static GenericValue executeFCMP_OEQ(GenericValue Src1, GenericValue Src2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 325 | Type *Ty) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 326 | GenericValue Dest; |
| 327 | switch (Ty->getTypeID()) { |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 328 | IMPLEMENT_FCMP(==, Float); |
| 329 | IMPLEMENT_FCMP(==, Double); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 330 | IMPLEMENT_VECTOR_FCMP(==); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 331 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 332 | dbgs() << "Unhandled type for FCmp EQ instruction: " << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 333 | llvm_unreachable(nullptr); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 334 | } |
| 335 | return Dest; |
| 336 | } |
| 337 | |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 338 | #define IMPLEMENT_SCALAR_NANS(TY, X,Y) \ |
| 339 | if (TY->isFloatTy()) { \ |
| 340 | if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \ |
| 341 | Dest.IntVal = APInt(1,false); \ |
| 342 | return Dest; \ |
| 343 | } \ |
| 344 | } else { \ |
| 345 | if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \ |
| 346 | Dest.IntVal = APInt(1,false); \ |
| 347 | return Dest; \ |
| 348 | } \ |
| 349 | } |
| 350 | |
| 351 | #define MASK_VECTOR_NANS_T(X,Y, TZ, FLAG) \ |
| 352 | assert(X.AggregateVal.size() == Y.AggregateVal.size()); \ |
| 353 | Dest.AggregateVal.resize( X.AggregateVal.size() ); \ |
| 354 | for( uint32_t _i=0;_i<X.AggregateVal.size();_i++) { \ |
| 355 | if (X.AggregateVal[_i].TZ##Val != X.AggregateVal[_i].TZ##Val || \ |
| 356 | Y.AggregateVal[_i].TZ##Val != Y.AggregateVal[_i].TZ##Val) \ |
| 357 | Dest.AggregateVal[_i].IntVal = APInt(1,FLAG); \ |
| 358 | else { \ |
| 359 | Dest.AggregateVal[_i].IntVal = APInt(1,!FLAG); \ |
| 360 | } \ |
| 361 | } |
| 362 | |
| 363 | #define MASK_VECTOR_NANS(TY, X,Y, FLAG) \ |
| 364 | if (TY->isVectorTy()) { \ |
| 365 | if (dyn_cast<VectorType>(TY)->getElementType()->isFloatTy()) { \ |
| 366 | MASK_VECTOR_NANS_T(X, Y, Float, FLAG) \ |
| 367 | } else { \ |
| 368 | MASK_VECTOR_NANS_T(X, Y, Double, FLAG) \ |
| 369 | } \ |
| 370 | } \ |
| 371 | |
| 372 | |
| 373 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 374 | static GenericValue executeFCMP_ONE(GenericValue Src1, GenericValue Src2, |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 375 | Type *Ty) |
| 376 | { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 377 | GenericValue Dest; |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 378 | // if input is scalar value and Src1 or Src2 is NaN return false |
| 379 | IMPLEMENT_SCALAR_NANS(Ty, Src1, Src2) |
| 380 | // if vector input detect NaNs and fill mask |
| 381 | MASK_VECTOR_NANS(Ty, Src1, Src2, false) |
| 382 | GenericValue DestMask = Dest; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 383 | switch (Ty->getTypeID()) { |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 384 | IMPLEMENT_FCMP(!=, Float); |
| 385 | IMPLEMENT_FCMP(!=, Double); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 386 | IMPLEMENT_VECTOR_FCMP(!=); |
| 387 | default: |
| 388 | dbgs() << "Unhandled type for FCmp NE instruction: " << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 389 | llvm_unreachable(nullptr); |
Nadav Rotem | 0fda0f3 | 2013-04-12 22:02:26 +0000 | [diff] [blame] | 390 | } |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 391 | // in vector case mask out NaN elements |
| 392 | if (Ty->isVectorTy()) |
| 393 | for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++) |
| 394 | if (DestMask.AggregateVal[_i].IntVal == false) |
| 395 | Dest.AggregateVal[_i].IntVal = APInt(1,false); |
| 396 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 397 | return Dest; |
| 398 | } |
| 399 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 400 | static GenericValue executeFCMP_OLE(GenericValue Src1, GenericValue Src2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 401 | Type *Ty) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 402 | GenericValue Dest; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 403 | switch (Ty->getTypeID()) { |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 404 | IMPLEMENT_FCMP(<=, Float); |
| 405 | IMPLEMENT_FCMP(<=, Double); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 406 | IMPLEMENT_VECTOR_FCMP(<=); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 407 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 408 | dbgs() << "Unhandled type for FCmp LE instruction: " << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 409 | llvm_unreachable(nullptr); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 410 | } |
| 411 | return Dest; |
| 412 | } |
| 413 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 414 | static GenericValue executeFCMP_OGE(GenericValue Src1, GenericValue Src2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 415 | Type *Ty) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 416 | GenericValue Dest; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 417 | switch (Ty->getTypeID()) { |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 418 | IMPLEMENT_FCMP(>=, Float); |
| 419 | IMPLEMENT_FCMP(>=, Double); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 420 | IMPLEMENT_VECTOR_FCMP(>=); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 421 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 422 | dbgs() << "Unhandled type for FCmp GE instruction: " << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 423 | llvm_unreachable(nullptr); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 424 | } |
| 425 | return Dest; |
| 426 | } |
| 427 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 428 | static GenericValue executeFCMP_OLT(GenericValue Src1, GenericValue Src2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 429 | Type *Ty) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 430 | GenericValue Dest; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 431 | switch (Ty->getTypeID()) { |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 432 | IMPLEMENT_FCMP(<, Float); |
| 433 | IMPLEMENT_FCMP(<, Double); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 434 | IMPLEMENT_VECTOR_FCMP(<); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 435 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 436 | dbgs() << "Unhandled type for FCmp LT instruction: " << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 437 | llvm_unreachable(nullptr); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 438 | } |
| 439 | return Dest; |
| 440 | } |
| 441 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 442 | static GenericValue executeFCMP_OGT(GenericValue Src1, GenericValue Src2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 443 | Type *Ty) { |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 444 | GenericValue Dest; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 445 | switch (Ty->getTypeID()) { |
Reid Spencer | e49661b | 2006-12-31 05:51:36 +0000 | [diff] [blame] | 446 | IMPLEMENT_FCMP(>, Float); |
| 447 | IMPLEMENT_FCMP(>, Double); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 448 | IMPLEMENT_VECTOR_FCMP(>); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 449 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 450 | dbgs() << "Unhandled type for FCmp GT instruction: " << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 451 | llvm_unreachable(nullptr); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 452 | } |
| 453 | return Dest; |
| 454 | } |
| 455 | |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 456 | #define IMPLEMENT_UNORDERED(TY, X,Y) \ |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 457 | if (TY->isFloatTy()) { \ |
Anton Korobeynikov | 4c71dfe | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 458 | if (X.FloatVal != X.FloatVal || Y.FloatVal != Y.FloatVal) { \ |
| 459 | Dest.IntVal = APInt(1,true); \ |
| 460 | return Dest; \ |
| 461 | } \ |
| 462 | } else if (X.DoubleVal != X.DoubleVal || Y.DoubleVal != Y.DoubleVal) { \ |
| 463 | Dest.IntVal = APInt(1,true); \ |
| 464 | return Dest; \ |
| 465 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 466 | |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 467 | #define IMPLEMENT_VECTOR_UNORDERED(TY, X,Y, _FUNC) \ |
| 468 | if (TY->isVectorTy()) { \ |
| 469 | GenericValue DestMask = Dest; \ |
| 470 | Dest = _FUNC(Src1, Src2, Ty); \ |
| 471 | for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++) \ |
| 472 | if (DestMask.AggregateVal[_i].IntVal == true) \ |
| 473 | Dest.AggregateVal[_i].IntVal = APInt(1,true); \ |
| 474 | return Dest; \ |
| 475 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 476 | |
| 477 | static GenericValue executeFCMP_UEQ(GenericValue Src1, GenericValue Src2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 478 | Type *Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 479 | GenericValue Dest; |
| 480 | IMPLEMENT_UNORDERED(Ty, Src1, Src2) |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 481 | MASK_VECTOR_NANS(Ty, Src1, Src2, true) |
| 482 | IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OEQ) |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 483 | return executeFCMP_OEQ(Src1, Src2, Ty); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 484 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | static GenericValue executeFCMP_UNE(GenericValue Src1, GenericValue Src2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 488 | Type *Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 489 | GenericValue Dest; |
| 490 | IMPLEMENT_UNORDERED(Ty, Src1, Src2) |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 491 | MASK_VECTOR_NANS(Ty, Src1, Src2, true) |
| 492 | IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_ONE) |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 493 | return executeFCMP_ONE(Src1, Src2, Ty); |
| 494 | } |
| 495 | |
| 496 | static GenericValue executeFCMP_ULE(GenericValue Src1, GenericValue Src2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 497 | Type *Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 498 | GenericValue Dest; |
| 499 | IMPLEMENT_UNORDERED(Ty, Src1, Src2) |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 500 | MASK_VECTOR_NANS(Ty, Src1, Src2, true) |
| 501 | IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OLE) |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 502 | return executeFCMP_OLE(Src1, Src2, Ty); |
| 503 | } |
| 504 | |
| 505 | static GenericValue executeFCMP_UGE(GenericValue Src1, GenericValue Src2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 506 | Type *Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 507 | GenericValue Dest; |
| 508 | IMPLEMENT_UNORDERED(Ty, Src1, Src2) |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 509 | MASK_VECTOR_NANS(Ty, Src1, Src2, true) |
| 510 | IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OGE) |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 511 | return executeFCMP_OGE(Src1, Src2, Ty); |
| 512 | } |
| 513 | |
| 514 | static GenericValue executeFCMP_ULT(GenericValue Src1, GenericValue Src2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 515 | Type *Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 516 | GenericValue Dest; |
| 517 | IMPLEMENT_UNORDERED(Ty, Src1, Src2) |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 518 | MASK_VECTOR_NANS(Ty, Src1, Src2, true) |
| 519 | IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OLT) |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 520 | return executeFCMP_OLT(Src1, Src2, Ty); |
| 521 | } |
| 522 | |
| 523 | static GenericValue executeFCMP_UGT(GenericValue Src1, GenericValue Src2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 524 | Type *Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 525 | GenericValue Dest; |
| 526 | IMPLEMENT_UNORDERED(Ty, Src1, Src2) |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 527 | MASK_VECTOR_NANS(Ty, Src1, Src2, true) |
| 528 | IMPLEMENT_VECTOR_UNORDERED(Ty, Src1, Src2, executeFCMP_OGT) |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 529 | return executeFCMP_OGT(Src1, Src2, Ty); |
| 530 | } |
| 531 | |
| 532 | static GenericValue executeFCMP_ORD(GenericValue Src1, GenericValue Src2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 533 | Type *Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 534 | GenericValue Dest; |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 535 | if(Ty->isVectorTy()) { |
| 536 | assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); |
| 537 | Dest.AggregateVal.resize( Src1.AggregateVal.size() ); |
| 538 | if(dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) { |
| 539 | for( size_t _i=0;_i<Src1.AggregateVal.size();_i++) |
| 540 | Dest.AggregateVal[_i].IntVal = APInt(1, |
| 541 | ( (Src1.AggregateVal[_i].FloatVal == |
| 542 | Src1.AggregateVal[_i].FloatVal) && |
| 543 | (Src2.AggregateVal[_i].FloatVal == |
| 544 | Src2.AggregateVal[_i].FloatVal))); |
| 545 | } else { |
| 546 | for( size_t _i=0;_i<Src1.AggregateVal.size();_i++) |
| 547 | Dest.AggregateVal[_i].IntVal = APInt(1, |
| 548 | ( (Src1.AggregateVal[_i].DoubleVal == |
| 549 | Src1.AggregateVal[_i].DoubleVal) && |
| 550 | (Src2.AggregateVal[_i].DoubleVal == |
| 551 | Src2.AggregateVal[_i].DoubleVal))); |
| 552 | } |
| 553 | } else if (Ty->isFloatTy()) |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 554 | Dest.IntVal = APInt(1,(Src1.FloatVal == Src1.FloatVal && |
| 555 | Src2.FloatVal == Src2.FloatVal)); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 556 | else { |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 557 | Dest.IntVal = APInt(1,(Src1.DoubleVal == Src1.DoubleVal && |
| 558 | Src2.DoubleVal == Src2.DoubleVal)); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 559 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 560 | return Dest; |
| 561 | } |
| 562 | |
| 563 | static GenericValue executeFCMP_UNO(GenericValue Src1, GenericValue Src2, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 564 | Type *Ty) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 565 | GenericValue Dest; |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 566 | if(Ty->isVectorTy()) { |
| 567 | assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); |
| 568 | Dest.AggregateVal.resize( Src1.AggregateVal.size() ); |
| 569 | if(dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) { |
| 570 | for( size_t _i=0;_i<Src1.AggregateVal.size();_i++) |
| 571 | Dest.AggregateVal[_i].IntVal = APInt(1, |
| 572 | ( (Src1.AggregateVal[_i].FloatVal != |
| 573 | Src1.AggregateVal[_i].FloatVal) || |
| 574 | (Src2.AggregateVal[_i].FloatVal != |
| 575 | Src2.AggregateVal[_i].FloatVal))); |
| 576 | } else { |
| 577 | for( size_t _i=0;_i<Src1.AggregateVal.size();_i++) |
| 578 | Dest.AggregateVal[_i].IntVal = APInt(1, |
| 579 | ( (Src1.AggregateVal[_i].DoubleVal != |
| 580 | Src1.AggregateVal[_i].DoubleVal) || |
| 581 | (Src2.AggregateVal[_i].DoubleVal != |
| 582 | Src2.AggregateVal[_i].DoubleVal))); |
| 583 | } |
| 584 | } else if (Ty->isFloatTy()) |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 585 | Dest.IntVal = APInt(1,(Src1.FloatVal != Src1.FloatVal || |
| 586 | Src2.FloatVal != Src2.FloatVal)); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 587 | else { |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 588 | Dest.IntVal = APInt(1,(Src1.DoubleVal != Src1.DoubleVal || |
| 589 | Src2.DoubleVal != Src2.DoubleVal)); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 590 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 591 | return Dest; |
| 592 | } |
| 593 | |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 594 | static GenericValue executeFCMP_BOOL(GenericValue Src1, GenericValue Src2, |
| 595 | const Type *Ty, const bool val) { |
| 596 | GenericValue Dest; |
| 597 | if(Ty->isVectorTy()) { |
| 598 | assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); |
| 599 | Dest.AggregateVal.resize( Src1.AggregateVal.size() ); |
| 600 | for( size_t _i=0; _i<Src1.AggregateVal.size(); _i++) |
| 601 | Dest.AggregateVal[_i].IntVal = APInt(1,val); |
| 602 | } else { |
| 603 | Dest.IntVal = APInt(1, val); |
| 604 | } |
| 605 | |
| 606 | return Dest; |
| 607 | } |
| 608 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 609 | void Interpreter::visitFCmpInst(FCmpInst &I) { |
| 610 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 611 | Type *Ty = I.getOperand(0)->getType(); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 612 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 613 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
| 614 | GenericValue R; // Result |
| 615 | |
| 616 | switch (I.getPredicate()) { |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 617 | default: |
| 618 | dbgs() << "Don't know how to handle this FCmp predicate!\n-->" << I; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 619 | llvm_unreachable(nullptr); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 620 | break; |
| 621 | case FCmpInst::FCMP_FALSE: R = executeFCMP_BOOL(Src1, Src2, Ty, false); |
| 622 | break; |
| 623 | case FCmpInst::FCMP_TRUE: R = executeFCMP_BOOL(Src1, Src2, Ty, true); |
| 624 | break; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 625 | case FCmpInst::FCMP_ORD: R = executeFCMP_ORD(Src1, Src2, Ty); break; |
| 626 | case FCmpInst::FCMP_UNO: R = executeFCMP_UNO(Src1, Src2, Ty); break; |
| 627 | case FCmpInst::FCMP_UEQ: R = executeFCMP_UEQ(Src1, Src2, Ty); break; |
| 628 | case FCmpInst::FCMP_OEQ: R = executeFCMP_OEQ(Src1, Src2, Ty); break; |
| 629 | case FCmpInst::FCMP_UNE: R = executeFCMP_UNE(Src1, Src2, Ty); break; |
| 630 | case FCmpInst::FCMP_ONE: R = executeFCMP_ONE(Src1, Src2, Ty); break; |
| 631 | case FCmpInst::FCMP_ULT: R = executeFCMP_ULT(Src1, Src2, Ty); break; |
| 632 | case FCmpInst::FCMP_OLT: R = executeFCMP_OLT(Src1, Src2, Ty); break; |
| 633 | case FCmpInst::FCMP_UGT: R = executeFCMP_UGT(Src1, Src2, Ty); break; |
| 634 | case FCmpInst::FCMP_OGT: R = executeFCMP_OGT(Src1, Src2, Ty); break; |
| 635 | case FCmpInst::FCMP_ULE: R = executeFCMP_ULE(Src1, Src2, Ty); break; |
| 636 | case FCmpInst::FCMP_OLE: R = executeFCMP_OLE(Src1, Src2, Ty); break; |
| 637 | case FCmpInst::FCMP_UGE: R = executeFCMP_UGE(Src1, Src2, Ty); break; |
| 638 | case FCmpInst::FCMP_OGE: R = executeFCMP_OGE(Src1, Src2, Ty); break; |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | SetValue(&I, R, SF); |
| 642 | } |
| 643 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 644 | static GenericValue executeCmpInst(unsigned predicate, GenericValue Src1, |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 645 | GenericValue Src2, Type *Ty) { |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 646 | GenericValue Result; |
| 647 | switch (predicate) { |
| 648 | case ICmpInst::ICMP_EQ: return executeICMP_EQ(Src1, Src2, Ty); |
| 649 | case ICmpInst::ICMP_NE: return executeICMP_NE(Src1, Src2, Ty); |
| 650 | case ICmpInst::ICMP_UGT: return executeICMP_UGT(Src1, Src2, Ty); |
| 651 | case ICmpInst::ICMP_SGT: return executeICMP_SGT(Src1, Src2, Ty); |
| 652 | case ICmpInst::ICMP_ULT: return executeICMP_ULT(Src1, Src2, Ty); |
| 653 | case ICmpInst::ICMP_SLT: return executeICMP_SLT(Src1, Src2, Ty); |
| 654 | case ICmpInst::ICMP_UGE: return executeICMP_UGE(Src1, Src2, Ty); |
| 655 | case ICmpInst::ICMP_SGE: return executeICMP_SGE(Src1, Src2, Ty); |
| 656 | case ICmpInst::ICMP_ULE: return executeICMP_ULE(Src1, Src2, Ty); |
| 657 | case ICmpInst::ICMP_SLE: return executeICMP_SLE(Src1, Src2, Ty); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 658 | case FCmpInst::FCMP_ORD: return executeFCMP_ORD(Src1, Src2, Ty); |
| 659 | case FCmpInst::FCMP_UNO: return executeFCMP_UNO(Src1, Src2, Ty); |
| 660 | case FCmpInst::FCMP_OEQ: return executeFCMP_OEQ(Src1, Src2, Ty); |
| 661 | case FCmpInst::FCMP_UEQ: return executeFCMP_UEQ(Src1, Src2, Ty); |
| 662 | case FCmpInst::FCMP_ONE: return executeFCMP_ONE(Src1, Src2, Ty); |
| 663 | case FCmpInst::FCMP_UNE: return executeFCMP_UNE(Src1, Src2, Ty); |
| 664 | case FCmpInst::FCMP_OLT: return executeFCMP_OLT(Src1, Src2, Ty); |
| 665 | case FCmpInst::FCMP_ULT: return executeFCMP_ULT(Src1, Src2, Ty); |
| 666 | case FCmpInst::FCMP_OGT: return executeFCMP_OGT(Src1, Src2, Ty); |
| 667 | case FCmpInst::FCMP_UGT: return executeFCMP_UGT(Src1, Src2, Ty); |
| 668 | case FCmpInst::FCMP_OLE: return executeFCMP_OLE(Src1, Src2, Ty); |
| 669 | case FCmpInst::FCMP_ULE: return executeFCMP_ULE(Src1, Src2, Ty); |
| 670 | case FCmpInst::FCMP_OGE: return executeFCMP_OGE(Src1, Src2, Ty); |
| 671 | case FCmpInst::FCMP_UGE: return executeFCMP_UGE(Src1, Src2, Ty); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 672 | case FCmpInst::FCMP_FALSE: return executeFCMP_BOOL(Src1, Src2, Ty, false); |
| 673 | case FCmpInst::FCMP_TRUE: return executeFCMP_BOOL(Src1, Src2, Ty, true); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 674 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 675 | dbgs() << "Unhandled Cmp predicate\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 676 | llvm_unreachable(nullptr); |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 677 | } |
| 678 | } |
| 679 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 680 | void Interpreter::visitBinaryOperator(BinaryOperator &I) { |
| 681 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 682 | Type *Ty = I.getOperand(0)->getType(); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 683 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 684 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 685 | GenericValue R; // Result |
| 686 | |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 687 | // First process vector operation |
| 688 | if (Ty->isVectorTy()) { |
| 689 | assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); |
| 690 | R.AggregateVal.resize(Src1.AggregateVal.size()); |
Nadav Rotem | 0fda0f3 | 2013-04-12 22:02:26 +0000 | [diff] [blame] | 691 | |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 692 | // Macros to execute binary operation 'OP' over integer vectors |
| 693 | #define INTEGER_VECTOR_OPERATION(OP) \ |
| 694 | for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \ |
| 695 | R.AggregateVal[i].IntVal = \ |
| 696 | Src1.AggregateVal[i].IntVal OP Src2.AggregateVal[i].IntVal; |
| 697 | |
| 698 | // Additional macros to execute binary operations udiv/sdiv/urem/srem since |
| 699 | // they have different notation. |
| 700 | #define INTEGER_VECTOR_FUNCTION(OP) \ |
| 701 | for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \ |
| 702 | R.AggregateVal[i].IntVal = \ |
| 703 | Src1.AggregateVal[i].IntVal.OP(Src2.AggregateVal[i].IntVal); |
| 704 | |
| 705 | // Macros to execute binary operation 'OP' over floating point type TY |
| 706 | // (float or double) vectors |
| 707 | #define FLOAT_VECTOR_FUNCTION(OP, TY) \ |
| 708 | for (unsigned i = 0; i < R.AggregateVal.size(); ++i) \ |
| 709 | R.AggregateVal[i].TY = \ |
| 710 | Src1.AggregateVal[i].TY OP Src2.AggregateVal[i].TY; |
| 711 | |
| 712 | // Macros to choose appropriate TY: float or double and run operation |
| 713 | // execution |
| 714 | #define FLOAT_VECTOR_OP(OP) { \ |
| 715 | if (dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) \ |
| 716 | FLOAT_VECTOR_FUNCTION(OP, FloatVal) \ |
| 717 | else { \ |
| 718 | if (dyn_cast<VectorType>(Ty)->getElementType()->isDoubleTy()) \ |
| 719 | FLOAT_VECTOR_FUNCTION(OP, DoubleVal) \ |
| 720 | else { \ |
| 721 | dbgs() << "Unhandled type for OP instruction: " << *Ty << "\n"; \ |
| 722 | llvm_unreachable(0); \ |
| 723 | } \ |
| 724 | } \ |
| 725 | } |
| 726 | |
| 727 | switch(I.getOpcode()){ |
| 728 | default: |
| 729 | dbgs() << "Don't know how to handle this binary operator!\n-->" << I; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 730 | llvm_unreachable(nullptr); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 731 | break; |
| 732 | case Instruction::Add: INTEGER_VECTOR_OPERATION(+) break; |
| 733 | case Instruction::Sub: INTEGER_VECTOR_OPERATION(-) break; |
| 734 | case Instruction::Mul: INTEGER_VECTOR_OPERATION(*) break; |
| 735 | case Instruction::UDiv: INTEGER_VECTOR_FUNCTION(udiv) break; |
| 736 | case Instruction::SDiv: INTEGER_VECTOR_FUNCTION(sdiv) break; |
| 737 | case Instruction::URem: INTEGER_VECTOR_FUNCTION(urem) break; |
| 738 | case Instruction::SRem: INTEGER_VECTOR_FUNCTION(srem) break; |
| 739 | case Instruction::And: INTEGER_VECTOR_OPERATION(&) break; |
| 740 | case Instruction::Or: INTEGER_VECTOR_OPERATION(|) break; |
| 741 | case Instruction::Xor: INTEGER_VECTOR_OPERATION(^) break; |
| 742 | case Instruction::FAdd: FLOAT_VECTOR_OP(+) break; |
| 743 | case Instruction::FSub: FLOAT_VECTOR_OP(-) break; |
| 744 | case Instruction::FMul: FLOAT_VECTOR_OP(*) break; |
| 745 | case Instruction::FDiv: FLOAT_VECTOR_OP(/) break; |
| 746 | case Instruction::FRem: |
| 747 | if (dyn_cast<VectorType>(Ty)->getElementType()->isFloatTy()) |
| 748 | for (unsigned i = 0; i < R.AggregateVal.size(); ++i) |
| 749 | R.AggregateVal[i].FloatVal = |
| 750 | fmod(Src1.AggregateVal[i].FloatVal, Src2.AggregateVal[i].FloatVal); |
| 751 | else { |
| 752 | if (dyn_cast<VectorType>(Ty)->getElementType()->isDoubleTy()) |
| 753 | for (unsigned i = 0; i < R.AggregateVal.size(); ++i) |
| 754 | R.AggregateVal[i].DoubleVal = |
| 755 | fmod(Src1.AggregateVal[i].DoubleVal, Src2.AggregateVal[i].DoubleVal); |
| 756 | else { |
| 757 | dbgs() << "Unhandled type for Rem instruction: " << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 758 | llvm_unreachable(nullptr); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | break; |
| 762 | } |
| 763 | } else { |
| 764 | switch (I.getOpcode()) { |
| 765 | default: |
| 766 | dbgs() << "Don't know how to handle this binary operator!\n-->" << I; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 767 | llvm_unreachable(nullptr); |
Nadav Rotem | 0d840bb | 2013-04-26 20:19:41 +0000 | [diff] [blame] | 768 | break; |
| 769 | case Instruction::Add: R.IntVal = Src1.IntVal + Src2.IntVal; break; |
| 770 | case Instruction::Sub: R.IntVal = Src1.IntVal - Src2.IntVal; break; |
| 771 | case Instruction::Mul: R.IntVal = Src1.IntVal * Src2.IntVal; break; |
| 772 | case Instruction::FAdd: executeFAddInst(R, Src1, Src2, Ty); break; |
| 773 | case Instruction::FSub: executeFSubInst(R, Src1, Src2, Ty); break; |
| 774 | case Instruction::FMul: executeFMulInst(R, Src1, Src2, Ty); break; |
| 775 | case Instruction::FDiv: executeFDivInst(R, Src1, Src2, Ty); break; |
| 776 | case Instruction::FRem: executeFRemInst(R, Src1, Src2, Ty); break; |
| 777 | case Instruction::UDiv: R.IntVal = Src1.IntVal.udiv(Src2.IntVal); break; |
| 778 | case Instruction::SDiv: R.IntVal = Src1.IntVal.sdiv(Src2.IntVal); break; |
| 779 | case Instruction::URem: R.IntVal = Src1.IntVal.urem(Src2.IntVal); break; |
| 780 | case Instruction::SRem: R.IntVal = Src1.IntVal.srem(Src2.IntVal); break; |
| 781 | case Instruction::And: R.IntVal = Src1.IntVal & Src2.IntVal; break; |
| 782 | case Instruction::Or: R.IntVal = Src1.IntVal | Src2.IntVal; break; |
| 783 | case Instruction::Xor: R.IntVal = Src1.IntVal ^ Src2.IntVal; break; |
| 784 | } |
| 785 | } |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 786 | SetValue(&I, R, SF); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 789 | static GenericValue executeSelectInst(GenericValue Src1, GenericValue Src2, |
Elena Demikhovsky | 4ca0ce2 | 2013-09-02 06:40:09 +0000 | [diff] [blame] | 790 | GenericValue Src3, const Type *Ty) { |
| 791 | GenericValue Dest; |
| 792 | if(Ty->isVectorTy()) { |
| 793 | assert(Src1.AggregateVal.size() == Src2.AggregateVal.size()); |
| 794 | assert(Src2.AggregateVal.size() == Src3.AggregateVal.size()); |
| 795 | Dest.AggregateVal.resize( Src1.AggregateVal.size() ); |
| 796 | for (size_t i = 0; i < Src1.AggregateVal.size(); ++i) |
| 797 | Dest.AggregateVal[i] = (Src1.AggregateVal[i].IntVal == 0) ? |
| 798 | Src3.AggregateVal[i] : Src2.AggregateVal[i]; |
| 799 | } else { |
| 800 | Dest = (Src1.IntVal == 0) ? Src3 : Src2; |
| 801 | } |
| 802 | return Dest; |
Chris Lattner | 759d34f | 2004-04-20 16:43:21 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | void Interpreter::visitSelectInst(SelectInst &I) { |
| 806 | ExecutionContext &SF = ECStack.back(); |
Elena Demikhovsky | 4ca0ce2 | 2013-09-02 06:40:09 +0000 | [diff] [blame] | 807 | const Type * Ty = I.getOperand(0)->getType(); |
Chris Lattner | 759d34f | 2004-04-20 16:43:21 +0000 | [diff] [blame] | 808 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 809 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
| 810 | GenericValue Src3 = getOperandValue(I.getOperand(2), SF); |
Elena Demikhovsky | 4ca0ce2 | 2013-09-02 06:40:09 +0000 | [diff] [blame] | 811 | GenericValue R = executeSelectInst(Src1, Src2, Src3, Ty); |
Chris Lattner | 759d34f | 2004-04-20 16:43:21 +0000 | [diff] [blame] | 812 | SetValue(&I, R, SF); |
| 813 | } |
| 814 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 815 | //===----------------------------------------------------------------------===// |
| 816 | // Terminator Instruction Implementations |
| 817 | //===----------------------------------------------------------------------===// |
| 818 | |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 819 | void Interpreter::exitCalled(GenericValue GV) { |
Brian Gaeke | d8400d8 | 2004-02-13 05:48:00 +0000 | [diff] [blame] | 820 | // runAtExitHandlers() assumes there are no stack frames, but |
| 821 | // if exit() was called, then it had a stack frame. Blow away |
| 822 | // the stack before interpreting atexit handlers. |
Chris Lattner | f32a6a3 | 2009-10-29 05:26:09 +0000 | [diff] [blame] | 823 | ECStack.clear(); |
| 824 | runAtExitHandlers(); |
| 825 | exit(GV.IntVal.zextOrTrunc(32).getZExtValue()); |
Chris Lattner | e43db88 | 2001-10-27 04:15:57 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Brian Gaeke | af955ba | 2003-11-07 05:22:49 +0000 | [diff] [blame] | 828 | /// Pop the last stack frame off of ECStack and then copy the result |
| 829 | /// back into the result variable if we are not returning void. The |
Jeff Cohen | 8c9191c | 2006-02-07 05:29:44 +0000 | [diff] [blame] | 830 | /// result variable may be the ExitValue, or the Value of the calling |
Brian Gaeke | dbde1ae | 2003-11-07 20:44:58 +0000 | [diff] [blame] | 831 | /// CallInst if there was a previous stack frame. This method may |
| 832 | /// invalidate any ECStack iterators you have. This method also takes |
| 833 | /// care of switching to the normal destination BB, if we are returning |
| 834 | /// from an invoke. |
Brian Gaeke | af955ba | 2003-11-07 05:22:49 +0000 | [diff] [blame] | 835 | /// |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 836 | void Interpreter::popStackAndReturnValueToCaller(Type *RetTy, |
Chris Lattner | f32a6a3 | 2009-10-29 05:26:09 +0000 | [diff] [blame] | 837 | GenericValue Result) { |
Brian Gaeke | af955ba | 2003-11-07 05:22:49 +0000 | [diff] [blame] | 838 | // Pop the current stack frame. |
| 839 | ECStack.pop_back(); |
| 840 | |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 841 | if (ECStack.empty()) { // Finished main. Put result into exit code... |
Dan Gohman | 9634331 | 2010-06-18 02:01:10 +0000 | [diff] [blame] | 842 | if (RetTy && !RetTy->isVoidTy()) { // Nonvoid return type? |
Jeff Cohen | 8c9191c | 2006-02-07 05:29:44 +0000 | [diff] [blame] | 843 | ExitValue = Result; // Capture the exit value of the program |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 844 | } else { |
Reid Spencer | e770787 | 2007-06-01 22:23:29 +0000 | [diff] [blame] | 845 | memset(&ExitValue.Untyped, 0, sizeof(ExitValue.Untyped)); |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 846 | } |
| 847 | } else { |
| 848 | // If we have a previous stack frame, and we have a previous call, |
| 849 | // fill in the return value... |
Brian Gaeke | af955ba | 2003-11-07 05:22:49 +0000 | [diff] [blame] | 850 | ExecutionContext &CallingSF = ECStack.back(); |
Brian Gaeke | dbde1ae | 2003-11-07 20:44:58 +0000 | [diff] [blame] | 851 | if (Instruction *I = CallingSF.Caller.getInstruction()) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 852 | // Save result... |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 853 | if (!CallingSF.Caller.getType()->isVoidTy()) |
Brian Gaeke | dbde1ae | 2003-11-07 20:44:58 +0000 | [diff] [blame] | 854 | SetValue(I, Result, CallingSF); |
| 855 | if (InvokeInst *II = dyn_cast<InvokeInst> (I)) |
| 856 | SwitchToNewBasicBlock (II->getNormalDest (), CallingSF); |
Brian Gaeke | 2cb474c | 2003-11-07 19:26:23 +0000 | [diff] [blame] | 857 | CallingSF.Caller = CallSite(); // We returned from the call... |
Brian Gaeke | af955ba | 2003-11-07 05:22:49 +0000 | [diff] [blame] | 858 | } |
| 859 | } |
| 860 | } |
| 861 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 862 | void Interpreter::visitReturnInst(ReturnInst &I) { |
| 863 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 864 | Type *RetTy = Type::getVoidTy(I.getContext()); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 865 | GenericValue Result; |
| 866 | |
| 867 | // Save away the return value... (if we are not 'ret void') |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 868 | if (I.getNumOperands()) { |
| 869 | RetTy = I.getReturnValue()->getType(); |
| 870 | Result = getOperandValue(I.getReturnValue(), SF); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Brian Gaeke | af955ba | 2003-11-07 05:22:49 +0000 | [diff] [blame] | 873 | popStackAndReturnValueToCaller(RetTy, Result); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Chris Lattner | ec7c1ab | 2004-10-16 18:21:33 +0000 | [diff] [blame] | 876 | void Interpreter::visitUnreachableInst(UnreachableInst &I) { |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 877 | report_fatal_error("Program executed an 'unreachable' instruction!"); |
Chris Lattner | ec7c1ab | 2004-10-16 18:21:33 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 880 | void Interpreter::visitBranchInst(BranchInst &I) { |
| 881 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 882 | BasicBlock *Dest; |
| 883 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 884 | Dest = I.getSuccessor(0); // Uncond branches have a fixed dest... |
| 885 | if (!I.isUnconditional()) { |
| 886 | Value *Cond = I.getCondition(); |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 887 | if (getOperandValue(Cond, SF).IntVal == 0) // If false cond... |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 888 | Dest = I.getSuccessor(1); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 889 | } |
Chris Lattner | 77113b6 | 2003-05-10 20:21:16 +0000 | [diff] [blame] | 890 | SwitchToNewBasicBlock(Dest, SF); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 893 | void Interpreter::visitSwitchInst(SwitchInst &I) { |
| 894 | ExecutionContext &SF = ECStack.back(); |
Eli Friedman | bb5a744 | 2011-09-29 20:21:17 +0000 | [diff] [blame] | 895 | Value* Cond = I.getCondition(); |
| 896 | Type *ElTy = Cond->getType(); |
| 897 | GenericValue CondVal = getOperandValue(Cond, SF); |
Chris Lattner | 09e9392 | 2003-04-22 20:34:47 +0000 | [diff] [blame] | 898 | |
| 899 | // Check to see if any of the cases match... |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 900 | BasicBlock *Dest = nullptr; |
Stepan Dyatkovskiy | 3d3abe0 | 2012-03-11 06:09:17 +0000 | [diff] [blame] | 901 | for (SwitchInst::CaseIt i = I.case_begin(), e = I.case_end(); i != e; ++i) { |
Bob Wilson | db3a9e6 | 2013-09-09 19:14:35 +0000 | [diff] [blame] | 902 | GenericValue CaseVal = getOperandValue(i.getCaseValue(), SF); |
| 903 | if (executeICMP_EQ(CondVal, CaseVal, ElTy).IntVal != 0) { |
| 904 | Dest = cast<BasicBlock>(i.getCaseSuccessor()); |
| 905 | break; |
Chris Lattner | 09e9392 | 2003-04-22 20:34:47 +0000 | [diff] [blame] | 906 | } |
Eli Friedman | bb5a744 | 2011-09-29 20:21:17 +0000 | [diff] [blame] | 907 | } |
Chris Lattner | 09e9392 | 2003-04-22 20:34:47 +0000 | [diff] [blame] | 908 | if (!Dest) Dest = I.getDefaultDest(); // No cases matched: use default |
Chris Lattner | 77113b6 | 2003-05-10 20:21:16 +0000 | [diff] [blame] | 909 | SwitchToNewBasicBlock(Dest, SF); |
| 910 | } |
| 911 | |
Chris Lattner | f32a6a3 | 2009-10-29 05:26:09 +0000 | [diff] [blame] | 912 | void Interpreter::visitIndirectBrInst(IndirectBrInst &I) { |
| 913 | ExecutionContext &SF = ECStack.back(); |
| 914 | void *Dest = GVTOP(getOperandValue(I.getAddress(), SF)); |
| 915 | SwitchToNewBasicBlock((BasicBlock*)Dest, SF); |
| 916 | } |
| 917 | |
| 918 | |
Chris Lattner | 77113b6 | 2003-05-10 20:21:16 +0000 | [diff] [blame] | 919 | // SwitchToNewBasicBlock - This method is used to jump to a new basic block. |
| 920 | // This function handles the actual updating of block and instruction iterators |
| 921 | // as well as execution of all of the PHI nodes in the destination block. |
| 922 | // |
| 923 | // This method does this because all of the PHI nodes must be executed |
| 924 | // atomically, reading their inputs before any of the results are updated. Not |
| 925 | // doing this can cause problems if the PHI nodes depend on other PHI nodes for |
| 926 | // their inputs. If the input PHI node is updated before it is read, incorrect |
| 927 | // results can happen. Thus we use a two phase approach. |
| 928 | // |
| 929 | void Interpreter::SwitchToNewBasicBlock(BasicBlock *Dest, ExecutionContext &SF){ |
| 930 | BasicBlock *PrevBB = SF.CurBB; // Remember where we came from... |
| 931 | SF.CurBB = Dest; // Update CurBB to branch destination |
| 932 | SF.CurInst = SF.CurBB->begin(); // Update new instruction ptr... |
| 933 | |
| 934 | if (!isa<PHINode>(SF.CurInst)) return; // Nothing fancy to do |
| 935 | |
| 936 | // Loop over all of the PHI nodes in the current block, reading their inputs. |
| 937 | std::vector<GenericValue> ResultValues; |
| 938 | |
| 939 | for (; PHINode *PN = dyn_cast<PHINode>(SF.CurInst); ++SF.CurInst) { |
| 940 | // Search for the value corresponding to this previous bb... |
| 941 | int i = PN->getBasicBlockIndex(PrevBB); |
| 942 | assert(i != -1 && "PHINode doesn't contain entry for predecessor??"); |
| 943 | Value *IncomingValue = PN->getIncomingValue(i); |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 944 | |
Chris Lattner | 77113b6 | 2003-05-10 20:21:16 +0000 | [diff] [blame] | 945 | // Save the incoming value for this PHI node... |
| 946 | ResultValues.push_back(getOperandValue(IncomingValue, SF)); |
| 947 | } |
| 948 | |
| 949 | // Now loop over all of the PHI nodes setting their values... |
| 950 | SF.CurInst = SF.CurBB->begin(); |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 951 | for (unsigned i = 0; isa<PHINode>(SF.CurInst); ++SF.CurInst, ++i) { |
| 952 | PHINode *PN = cast<PHINode>(SF.CurInst); |
Chris Lattner | 77113b6 | 2003-05-10 20:21:16 +0000 | [diff] [blame] | 953 | SetValue(PN, ResultValues[i], SF); |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 954 | } |
Chris Lattner | 09e9392 | 2003-04-22 20:34:47 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 957 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 958 | // Memory Instruction Implementations |
| 959 | //===----------------------------------------------------------------------===// |
| 960 | |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 961 | void Interpreter::visitAllocaInst(AllocaInst &I) { |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 962 | ExecutionContext &SF = ECStack.back(); |
| 963 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 964 | Type *Ty = I.getType()->getElementType(); // Type to be allocated |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 965 | |
Chris Lattner | cc82cc1 | 2002-04-28 21:57:33 +0000 | [diff] [blame] | 966 | // Get the number of elements being allocated by the array... |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 967 | unsigned NumElements = |
| 968 | getOperandValue(I.getOperand(0), SF).IntVal.getZExtValue(); |
| 969 | |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 970 | unsigned TypeSize = (size_t)TD.getTypeAllocSize(Ty); |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 971 | |
Gabor Greif | 724441e | 2007-10-11 19:40:35 +0000 | [diff] [blame] | 972 | // Avoid malloc-ing zero bytes, use max()... |
| 973 | unsigned MemToAlloc = std::max(1U, NumElements * TypeSize); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 974 | |
| 975 | // Allocate enough memory to hold the type... |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 976 | void *Memory = malloc(MemToAlloc); |
| 977 | |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 978 | DEBUG(dbgs() << "Allocated Type: " << *Ty << " (" << TypeSize << " bytes) x " |
Chris Lattner | 79c5d3f | 2009-08-23 04:52:46 +0000 | [diff] [blame] | 979 | << NumElements << " (Total: " << MemToAlloc << ") at " |
| 980 | << uintptr_t(Memory) << '\n'); |
Chris Lattner | 9bffa73 | 2002-02-19 18:50:09 +0000 | [diff] [blame] | 981 | |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 982 | GenericValue Result = PTOGV(Memory); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 983 | assert(Result.PointerVal && "Null pointer returned by malloc!"); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 984 | SetValue(&I, Result, SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 985 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 986 | if (I.getOpcode() == Instruction::Alloca) |
Chris Lattner | 9bffa73 | 2002-02-19 18:50:09 +0000 | [diff] [blame] | 987 | ECStack.back().Allocas.add(Memory); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 990 | // getElementOffset - The workhorse for getelementptr. |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 991 | // |
Chris Lattner | 4af6de8 | 2003-11-25 20:44:56 +0000 | [diff] [blame] | 992 | GenericValue Interpreter::executeGEPOperation(Value *Ptr, gep_type_iterator I, |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 993 | gep_type_iterator E, |
| 994 | ExecutionContext &SF) { |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 995 | assert(Ptr->getType()->isPointerTy() && |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 996 | "Cannot getElementOffset of a nonpointer type!"); |
| 997 | |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 998 | uint64_t Total = 0; |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 999 | |
| 1000 | for (; I != E; ++I) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1001 | if (StructType *STy = dyn_cast<StructType>(*I)) { |
Chris Lattner | 782b939 | 2001-11-26 18:18:18 +0000 | [diff] [blame] | 1002 | const StructLayout *SLO = TD.getStructLayout(STy); |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 1003 | |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 1004 | const ConstantInt *CPU = cast<ConstantInt>(I.getOperand()); |
| 1005 | unsigned Index = unsigned(CPU->getZExtValue()); |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 1006 | |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 1007 | Total += SLO->getElementOffset(Index); |
Chris Lattner | 4af6de8 | 2003-11-25 20:44:56 +0000 | [diff] [blame] | 1008 | } else { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1009 | SequentialType *ST = cast<SequentialType>(*I); |
Chris Lattner | 006a4a5 | 2003-02-25 21:14:59 +0000 | [diff] [blame] | 1010 | // Get the index number for the array... which must be long type... |
Chris Lattner | 4af6de8 | 2003-11-25 20:44:56 +0000 | [diff] [blame] | 1011 | GenericValue IdxGV = getOperandValue(I.getOperand(), SF); |
| 1012 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1013 | int64_t Idx; |
| 1014 | unsigned BitWidth = |
| 1015 | cast<IntegerType>(I.getOperand()->getType())->getBitWidth(); |
Reid Spencer | 23e2883 | 2007-01-18 01:25:42 +0000 | [diff] [blame] | 1016 | if (BitWidth == 32) |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 1017 | Idx = (int64_t)(int32_t)IdxGV.IntVal.getZExtValue(); |
Chris Lattner | aee7f21 | 2008-04-06 21:50:58 +0000 | [diff] [blame] | 1018 | else { |
| 1019 | assert(BitWidth == 64 && "Invalid index type for getelementptr"); |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 1020 | Idx = (int64_t)IdxGV.IntVal.getZExtValue(); |
Chris Lattner | aee7f21 | 2008-04-06 21:50:58 +0000 | [diff] [blame] | 1021 | } |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 1022 | Total += TD.getTypeAllocSize(ST->getElementType())*Idx; |
Brian Gaeke | 03e43dc | 2003-10-24 19:59:01 +0000 | [diff] [blame] | 1023 | } |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 1026 | GenericValue Result; |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 1027 | Result.PointerVal = ((char*)getOperandValue(Ptr, SF).PointerVal) + Total; |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 1028 | DEBUG(dbgs() << "GEP Index " << Total << " bytes.\n"); |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 1029 | return Result; |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 1032 | void Interpreter::visitGetElementPtrInst(GetElementPtrInst &I) { |
| 1033 | ExecutionContext &SF = ECStack.back(); |
Owen Anderson | fdca74c | 2009-06-26 16:46:15 +0000 | [diff] [blame] | 1034 | SetValue(&I, executeGEPOperation(I.getPointerOperand(), |
Chris Lattner | 4af6de8 | 2003-11-25 20:44:56 +0000 | [diff] [blame] | 1035 | gep_type_begin(I), gep_type_end(I), SF), SF); |
Chris Lattner | 95c3af5 | 2001-10-29 19:32:19 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 1038 | void Interpreter::visitLoadInst(LoadInst &I) { |
| 1039 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1040 | GenericValue SRC = getOperandValue(I.getPointerOperand(), SF); |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 1041 | GenericValue *Ptr = (GenericValue*)GVTOP(SRC); |
Reid Spencer | e092936 | 2007-03-03 08:38:04 +0000 | [diff] [blame] | 1042 | GenericValue Result; |
Reid Spencer | e092936 | 2007-03-03 08:38:04 +0000 | [diff] [blame] | 1043 | LoadValueFromMemory(Result, Ptr, I.getType()); |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1044 | SetValue(&I, Result, SF); |
Chris Lattner | 663ceeb | 2008-07-08 17:25:49 +0000 | [diff] [blame] | 1045 | if (I.isVolatile() && PrintVolatile) |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 1046 | dbgs() << "Volatile load " << I; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 1049 | void Interpreter::visitStoreInst(StoreInst &I) { |
| 1050 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | fddc755 | 2002-10-15 20:34:05 +0000 | [diff] [blame] | 1051 | GenericValue Val = getOperandValue(I.getOperand(0), SF); |
| 1052 | GenericValue SRC = getOperandValue(I.getPointerOperand(), SF); |
Chris Lattner | fe11a97 | 2002-12-23 23:59:41 +0000 | [diff] [blame] | 1053 | StoreValueToMemory(Val, (GenericValue *)GVTOP(SRC), |
Chris Lattner | 683d5da9 | 2002-10-26 01:57:15 +0000 | [diff] [blame] | 1054 | I.getOperand(0)->getType()); |
Chris Lattner | 663ceeb | 2008-07-08 17:25:49 +0000 | [diff] [blame] | 1055 | if (I.isVolatile() && PrintVolatile) |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 1056 | dbgs() << "Volatile store: " << I; |
Chris Lattner | fddc755 | 2002-10-15 20:34:05 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1059 | //===----------------------------------------------------------------------===// |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1060 | // Miscellaneous Instruction Implementations |
| 1061 | //===----------------------------------------------------------------------===// |
| 1062 | |
Brian Gaeke | fea483d | 2003-11-07 20:04:22 +0000 | [diff] [blame] | 1063 | void Interpreter::visitCallSite(CallSite CS) { |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 1064 | ExecutionContext &SF = ECStack.back(); |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 1065 | |
| 1066 | // Check to see if this is an intrinsic function call... |
Reid Spencer | 4ccf462 | 2007-04-16 21:50:40 +0000 | [diff] [blame] | 1067 | Function *F = CS.getCalledFunction(); |
Chris Lattner | f32a6a3 | 2009-10-29 05:26:09 +0000 | [diff] [blame] | 1068 | if (F && F->isDeclaration()) |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 1069 | switch (F->getIntrinsicID()) { |
Brian Gaeke | 34562ba | 2004-01-14 06:02:53 +0000 | [diff] [blame] | 1070 | case Intrinsic::not_intrinsic: |
| 1071 | break; |
Chris Lattner | 317201d | 2004-03-13 00:24:00 +0000 | [diff] [blame] | 1072 | case Intrinsic::vastart: { // va_start |
Brian Gaeke | 9d20b71 | 2004-02-25 23:01:48 +0000 | [diff] [blame] | 1073 | GenericValue ArgIndex; |
| 1074 | ArgIndex.UIntPairVal.first = ECStack.size() - 1; |
| 1075 | ArgIndex.UIntPairVal.second = 0; |
| 1076 | SetValue(CS.getInstruction(), ArgIndex, SF); |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 1077 | return; |
Brian Gaeke | 9d20b71 | 2004-02-25 23:01:48 +0000 | [diff] [blame] | 1078 | } |
Chris Lattner | 317201d | 2004-03-13 00:24:00 +0000 | [diff] [blame] | 1079 | case Intrinsic::vaend: // va_end is a noop for the interpreter |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 1080 | return; |
Chris Lattner | 317201d | 2004-03-13 00:24:00 +0000 | [diff] [blame] | 1081 | case Intrinsic::vacopy: // va_copy: dest = src |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 1082 | SetValue(CS.getInstruction(), getOperandValue(*CS.arg_begin(), SF), SF); |
| 1083 | return; |
| 1084 | default: |
Brian Gaeke | 34562ba | 2004-01-14 06:02:53 +0000 | [diff] [blame] | 1085 | // If it is an unknown intrinsic function, use the intrinsic lowering |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 1086 | // class to transform it into hopefully tasty LLVM code. |
| 1087 | // |
Chris Lattner | b8e237b | 2007-04-17 17:38:28 +0000 | [diff] [blame] | 1088 | BasicBlock::iterator me(CS.getInstruction()); |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 1089 | BasicBlock *Parent = CS.getInstruction()->getParent(); |
Chris Lattner | b8e237b | 2007-04-17 17:38:28 +0000 | [diff] [blame] | 1090 | bool atBegin(Parent->begin() == me); |
| 1091 | if (!atBegin) |
| 1092 | --me; |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 1093 | IL->LowerIntrinsicCall(cast<CallInst>(CS.getInstruction())); |
| 1094 | |
| 1095 | // Restore the CurInst pointer to the first instruction newly inserted, if |
| 1096 | // any. |
Chris Lattner | b8e237b | 2007-04-17 17:38:28 +0000 | [diff] [blame] | 1097 | if (atBegin) { |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 1098 | SF.CurInst = Parent->begin(); |
| 1099 | } else { |
Chris Lattner | b8e237b | 2007-04-17 17:38:28 +0000 | [diff] [blame] | 1100 | SF.CurInst = me; |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 1101 | ++SF.CurInst; |
| 1102 | } |
Brian Gaeke | b440dea | 2004-04-23 18:05:28 +0000 | [diff] [blame] | 1103 | return; |
Chris Lattner | 7301178 | 2003-12-28 09:44:37 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Reid Spencer | 4ccf462 | 2007-04-16 21:50:40 +0000 | [diff] [blame] | 1106 | |
Brian Gaeke | fea483d | 2003-11-07 20:04:22 +0000 | [diff] [blame] | 1107 | SF.Caller = CS; |
Chris Lattner | 0286835 | 2003-04-22 21:22:33 +0000 | [diff] [blame] | 1108 | std::vector<GenericValue> ArgVals; |
Brian Gaeke | ae2495a | 2003-11-07 19:59:08 +0000 | [diff] [blame] | 1109 | const unsigned NumArgs = SF.Caller.arg_size(); |
| 1110 | ArgVals.reserve(NumArgs); |
Reid Spencer | 4ccf462 | 2007-04-16 21:50:40 +0000 | [diff] [blame] | 1111 | uint16_t pNum = 1; |
Brian Gaeke | ae2495a | 2003-11-07 19:59:08 +0000 | [diff] [blame] | 1112 | for (CallSite::arg_iterator i = SF.Caller.arg_begin(), |
Reid Spencer | 4ccf462 | 2007-04-16 21:50:40 +0000 | [diff] [blame] | 1113 | e = SF.Caller.arg_end(); i != e; ++i, ++pNum) { |
Brian Gaeke | ae2495a | 2003-11-07 19:59:08 +0000 | [diff] [blame] | 1114 | Value *V = *i; |
| 1115 | ArgVals.push_back(getOperandValue(V, SF)); |
Chris Lattner | 9378013 | 2003-01-13 00:58:52 +0000 | [diff] [blame] | 1116 | } |
Chris Lattner | 365a76e | 2001-09-10 04:49:44 +0000 | [diff] [blame] | 1117 | |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 1118 | // To handle indirect calls, we must get the pointer value from the argument |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 1119 | // and treat it as a function pointer. |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 1120 | GenericValue SRC = getOperandValue(SF.Caller.getCalledValue(), SF); |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 1121 | callFunction((Function*)GVTOP(SRC), ArgVals); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 1124 | // auxiliary function for shift operations |
Elena Demikhovsky | cf21d15 | 2013-08-05 12:17:06 +0000 | [diff] [blame] | 1125 | static unsigned getShiftAmount(uint64_t orgShiftAmount, |
| 1126 | llvm::APInt valueToShift) { |
| 1127 | unsigned valueWidth = valueToShift.getBitWidth(); |
| 1128 | if (orgShiftAmount < (uint64_t)valueWidth) |
| 1129 | return orgShiftAmount; |
| 1130 | // according to the llvm documentation, if orgShiftAmount > valueWidth, |
| 1131 | // the result is undfeined. but we do shift by this rule: |
| 1132 | return (NextPowerOf2(valueWidth-1) - 1) & orgShiftAmount; |
| 1133 | } |
| 1134 | |
| 1135 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1136 | void Interpreter::visitShl(BinaryOperator &I) { |
Brian Gaeke | 63438cc | 2003-12-11 00:22:59 +0000 | [diff] [blame] | 1137 | ExecutionContext &SF = ECStack.back(); |
Brian Gaeke | 63438cc | 2003-12-11 00:22:59 +0000 | [diff] [blame] | 1138 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 1139 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
| 1140 | GenericValue Dest; |
Elena Demikhovsky | cf21d15 | 2013-08-05 12:17:06 +0000 | [diff] [blame] | 1141 | const Type *Ty = I.getType(); |
| 1142 | |
| 1143 | if (Ty->isVectorTy()) { |
| 1144 | uint32_t src1Size = uint32_t(Src1.AggregateVal.size()); |
| 1145 | assert(src1Size == Src2.AggregateVal.size()); |
| 1146 | for (unsigned i = 0; i < src1Size; i++) { |
| 1147 | GenericValue Result; |
| 1148 | uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue(); |
| 1149 | llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal; |
| 1150 | Result.IntVal = valueToShift.shl(getShiftAmount(shiftAmount, valueToShift)); |
| 1151 | Dest.AggregateVal.push_back(Result); |
| 1152 | } |
| 1153 | } else { |
| 1154 | // scalar |
| 1155 | uint64_t shiftAmount = Src2.IntVal.getZExtValue(); |
| 1156 | llvm::APInt valueToShift = Src1.IntVal; |
| 1157 | Dest.IntVal = valueToShift.shl(getShiftAmount(shiftAmount, valueToShift)); |
| 1158 | } |
| 1159 | |
Brian Gaeke | 63438cc | 2003-12-11 00:22:59 +0000 | [diff] [blame] | 1160 | SetValue(&I, Dest, SF); |
| 1161 | } |
| 1162 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1163 | void Interpreter::visitLShr(BinaryOperator &I) { |
Brian Gaeke | 63438cc | 2003-12-11 00:22:59 +0000 | [diff] [blame] | 1164 | ExecutionContext &SF = ECStack.back(); |
Brian Gaeke | 63438cc | 2003-12-11 00:22:59 +0000 | [diff] [blame] | 1165 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 1166 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
| 1167 | GenericValue Dest; |
Elena Demikhovsky | cf21d15 | 2013-08-05 12:17:06 +0000 | [diff] [blame] | 1168 | const Type *Ty = I.getType(); |
| 1169 | |
| 1170 | if (Ty->isVectorTy()) { |
| 1171 | uint32_t src1Size = uint32_t(Src1.AggregateVal.size()); |
| 1172 | assert(src1Size == Src2.AggregateVal.size()); |
| 1173 | for (unsigned i = 0; i < src1Size; i++) { |
| 1174 | GenericValue Result; |
| 1175 | uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue(); |
| 1176 | llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal; |
| 1177 | Result.IntVal = valueToShift.lshr(getShiftAmount(shiftAmount, valueToShift)); |
| 1178 | Dest.AggregateVal.push_back(Result); |
| 1179 | } |
| 1180 | } else { |
| 1181 | // scalar |
| 1182 | uint64_t shiftAmount = Src2.IntVal.getZExtValue(); |
| 1183 | llvm::APInt valueToShift = Src1.IntVal; |
| 1184 | Dest.IntVal = valueToShift.lshr(getShiftAmount(shiftAmount, valueToShift)); |
| 1185 | } |
| 1186 | |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1187 | SetValue(&I, Dest, SF); |
| 1188 | } |
| 1189 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1190 | void Interpreter::visitAShr(BinaryOperator &I) { |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1191 | ExecutionContext &SF = ECStack.back(); |
Reid Spencer | 3822ff5 | 2006-11-08 06:47:33 +0000 | [diff] [blame] | 1192 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 1193 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
Chris Lattner | 9029cb8 | 2009-01-16 20:17:02 +0000 | [diff] [blame] | 1194 | GenericValue Dest; |
Elena Demikhovsky | cf21d15 | 2013-08-05 12:17:06 +0000 | [diff] [blame] | 1195 | const Type *Ty = I.getType(); |
| 1196 | |
| 1197 | if (Ty->isVectorTy()) { |
| 1198 | size_t src1Size = Src1.AggregateVal.size(); |
| 1199 | assert(src1Size == Src2.AggregateVal.size()); |
| 1200 | for (unsigned i = 0; i < src1Size; i++) { |
| 1201 | GenericValue Result; |
| 1202 | uint64_t shiftAmount = Src2.AggregateVal[i].IntVal.getZExtValue(); |
| 1203 | llvm::APInt valueToShift = Src1.AggregateVal[i].IntVal; |
| 1204 | Result.IntVal = valueToShift.ashr(getShiftAmount(shiftAmount, valueToShift)); |
| 1205 | Dest.AggregateVal.push_back(Result); |
| 1206 | } |
| 1207 | } else { |
| 1208 | // scalar |
| 1209 | uint64_t shiftAmount = Src2.IntVal.getZExtValue(); |
| 1210 | llvm::APInt valueToShift = Src1.IntVal; |
| 1211 | Dest.IntVal = valueToShift.ashr(getShiftAmount(shiftAmount, valueToShift)); |
| 1212 | } |
| 1213 | |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 1214 | SetValue(&I, Dest, SF); |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1217 | GenericValue Interpreter::executeTruncInst(Value *SrcVal, Type *DstTy, |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1218 | ExecutionContext &SF) { |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 1219 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
Elena Demikhovsky | cf21d15 | 2013-08-05 12:17:06 +0000 | [diff] [blame] | 1220 | Type *SrcTy = SrcVal->getType(); |
| 1221 | if (SrcTy->isVectorTy()) { |
| 1222 | Type *DstVecTy = DstTy->getScalarType(); |
| 1223 | unsigned DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth(); |
| 1224 | unsigned NumElts = Src.AggregateVal.size(); |
| 1225 | // the sizes of src and dst vectors must be equal |
| 1226 | Dest.AggregateVal.resize(NumElts); |
| 1227 | for (unsigned i = 0; i < NumElts; i++) |
| 1228 | Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.trunc(DBitWidth); |
| 1229 | } else { |
| 1230 | IntegerType *DITy = cast<IntegerType>(DstTy); |
| 1231 | unsigned DBitWidth = DITy->getBitWidth(); |
| 1232 | Dest.IntVal = Src.IntVal.trunc(DBitWidth); |
| 1233 | } |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 1234 | return Dest; |
Chris Lattner | 8666098 | 2001-08-27 05:16:50 +0000 | [diff] [blame] | 1235 | } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1236 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1237 | GenericValue Interpreter::executeSExtInst(Value *SrcVal, Type *DstTy, |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1238 | ExecutionContext &SF) { |
Elena Demikhovsky | cf21d15 | 2013-08-05 12:17:06 +0000 | [diff] [blame] | 1239 | const Type *SrcTy = SrcVal->getType(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1240 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
Elena Demikhovsky | cf21d15 | 2013-08-05 12:17:06 +0000 | [diff] [blame] | 1241 | if (SrcTy->isVectorTy()) { |
| 1242 | const Type *DstVecTy = DstTy->getScalarType(); |
| 1243 | unsigned DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth(); |
| 1244 | unsigned size = Src.AggregateVal.size(); |
| 1245 | // the sizes of src and dst vectors must be equal. |
| 1246 | Dest.AggregateVal.resize(size); |
| 1247 | for (unsigned i = 0; i < size; i++) |
| 1248 | Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.sext(DBitWidth); |
| 1249 | } else { |
| 1250 | const IntegerType *DITy = cast<IntegerType>(DstTy); |
| 1251 | unsigned DBitWidth = DITy->getBitWidth(); |
| 1252 | Dest.IntVal = Src.IntVal.sext(DBitWidth); |
| 1253 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1254 | return Dest; |
| 1255 | } |
| 1256 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1257 | GenericValue Interpreter::executeZExtInst(Value *SrcVal, Type *DstTy, |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1258 | ExecutionContext &SF) { |
Elena Demikhovsky | cf21d15 | 2013-08-05 12:17:06 +0000 | [diff] [blame] | 1259 | const Type *SrcTy = SrcVal->getType(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1260 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
Elena Demikhovsky | cf21d15 | 2013-08-05 12:17:06 +0000 | [diff] [blame] | 1261 | if (SrcTy->isVectorTy()) { |
| 1262 | const Type *DstVecTy = DstTy->getScalarType(); |
| 1263 | unsigned DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth(); |
| 1264 | |
| 1265 | unsigned size = Src.AggregateVal.size(); |
| 1266 | // the sizes of src and dst vectors must be equal. |
| 1267 | Dest.AggregateVal.resize(size); |
| 1268 | for (unsigned i = 0; i < size; i++) |
| 1269 | Dest.AggregateVal[i].IntVal = Src.AggregateVal[i].IntVal.zext(DBitWidth); |
| 1270 | } else { |
| 1271 | const IntegerType *DITy = cast<IntegerType>(DstTy); |
| 1272 | unsigned DBitWidth = DITy->getBitWidth(); |
| 1273 | Dest.IntVal = Src.IntVal.zext(DBitWidth); |
| 1274 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1275 | return Dest; |
| 1276 | } |
| 1277 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1278 | GenericValue Interpreter::executeFPTruncInst(Value *SrcVal, Type *DstTy, |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1279 | ExecutionContext &SF) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1280 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
Elena Demikhovsky | cf21d15 | 2013-08-05 12:17:06 +0000 | [diff] [blame] | 1281 | |
| 1282 | if (SrcVal->getType()->getTypeID() == Type::VectorTyID) { |
| 1283 | assert(SrcVal->getType()->getScalarType()->isDoubleTy() && |
| 1284 | DstTy->getScalarType()->isFloatTy() && |
| 1285 | "Invalid FPTrunc instruction"); |
| 1286 | |
| 1287 | unsigned size = Src.AggregateVal.size(); |
| 1288 | // the sizes of src and dst vectors must be equal. |
| 1289 | Dest.AggregateVal.resize(size); |
| 1290 | for (unsigned i = 0; i < size; i++) |
| 1291 | Dest.AggregateVal[i].FloatVal = (float)Src.AggregateVal[i].DoubleVal; |
| 1292 | } else { |
| 1293 | assert(SrcVal->getType()->isDoubleTy() && DstTy->isFloatTy() && |
| 1294 | "Invalid FPTrunc instruction"); |
| 1295 | Dest.FloatVal = (float)Src.DoubleVal; |
| 1296 | } |
| 1297 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1298 | return Dest; |
| 1299 | } |
| 1300 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1301 | GenericValue Interpreter::executeFPExtInst(Value *SrcVal, Type *DstTy, |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1302 | ExecutionContext &SF) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1303 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
Elena Demikhovsky | cf21d15 | 2013-08-05 12:17:06 +0000 | [diff] [blame] | 1304 | |
| 1305 | if (SrcVal->getType()->getTypeID() == Type::VectorTyID) { |
| 1306 | assert(SrcVal->getType()->getScalarType()->isFloatTy() && |
| 1307 | DstTy->getScalarType()->isDoubleTy() && "Invalid FPExt instruction"); |
| 1308 | |
| 1309 | unsigned size = Src.AggregateVal.size(); |
| 1310 | // the sizes of src and dst vectors must be equal. |
| 1311 | Dest.AggregateVal.resize(size); |
| 1312 | for (unsigned i = 0; i < size; i++) |
| 1313 | Dest.AggregateVal[i].DoubleVal = (double)Src.AggregateVal[i].FloatVal; |
| 1314 | } else { |
| 1315 | assert(SrcVal->getType()->isFloatTy() && DstTy->isDoubleTy() && |
| 1316 | "Invalid FPExt instruction"); |
| 1317 | Dest.DoubleVal = (double)Src.FloatVal; |
| 1318 | } |
| 1319 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1320 | return Dest; |
| 1321 | } |
| 1322 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1323 | GenericValue Interpreter::executeFPToUIInst(Value *SrcVal, Type *DstTy, |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1324 | ExecutionContext &SF) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1325 | Type *SrcTy = SrcVal->getType(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1326 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
Reid Spencer | e092936 | 2007-03-03 08:38:04 +0000 | [diff] [blame] | 1327 | |
Elena Demikhovsky | cf21d15 | 2013-08-05 12:17:06 +0000 | [diff] [blame] | 1328 | if (SrcTy->getTypeID() == Type::VectorTyID) { |
| 1329 | const Type *DstVecTy = DstTy->getScalarType(); |
| 1330 | const Type *SrcVecTy = SrcTy->getScalarType(); |
| 1331 | uint32_t DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth(); |
| 1332 | unsigned size = Src.AggregateVal.size(); |
| 1333 | // the sizes of src and dst vectors must be equal. |
| 1334 | Dest.AggregateVal.resize(size); |
| 1335 | |
| 1336 | if (SrcVecTy->getTypeID() == Type::FloatTyID) { |
| 1337 | assert(SrcVecTy->isFloatingPointTy() && "Invalid FPToUI instruction"); |
| 1338 | for (unsigned i = 0; i < size; i++) |
| 1339 | Dest.AggregateVal[i].IntVal = APIntOps::RoundFloatToAPInt( |
| 1340 | Src.AggregateVal[i].FloatVal, DBitWidth); |
| 1341 | } else { |
| 1342 | for (unsigned i = 0; i < size; i++) |
| 1343 | Dest.AggregateVal[i].IntVal = APIntOps::RoundDoubleToAPInt( |
| 1344 | Src.AggregateVal[i].DoubleVal, DBitWidth); |
| 1345 | } |
| 1346 | } else { |
| 1347 | // scalar |
| 1348 | uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth(); |
| 1349 | assert(SrcTy->isFloatingPointTy() && "Invalid FPToUI instruction"); |
| 1350 | |
| 1351 | if (SrcTy->getTypeID() == Type::FloatTyID) |
| 1352 | Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth); |
| 1353 | else { |
| 1354 | Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth); |
| 1355 | } |
| 1356 | } |
| 1357 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1358 | return Dest; |
| 1359 | } |
| 1360 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1361 | GenericValue Interpreter::executeFPToSIInst(Value *SrcVal, Type *DstTy, |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1362 | ExecutionContext &SF) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1363 | Type *SrcTy = SrcVal->getType(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1364 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
Reid Spencer | e092936 | 2007-03-03 08:38:04 +0000 | [diff] [blame] | 1365 | |
Elena Demikhovsky | cf21d15 | 2013-08-05 12:17:06 +0000 | [diff] [blame] | 1366 | if (SrcTy->getTypeID() == Type::VectorTyID) { |
| 1367 | const Type *DstVecTy = DstTy->getScalarType(); |
| 1368 | const Type *SrcVecTy = SrcTy->getScalarType(); |
| 1369 | uint32_t DBitWidth = cast<IntegerType>(DstVecTy)->getBitWidth(); |
| 1370 | unsigned size = Src.AggregateVal.size(); |
| 1371 | // the sizes of src and dst vectors must be equal |
| 1372 | Dest.AggregateVal.resize(size); |
| 1373 | |
| 1374 | if (SrcVecTy->getTypeID() == Type::FloatTyID) { |
| 1375 | assert(SrcVecTy->isFloatingPointTy() && "Invalid FPToSI instruction"); |
| 1376 | for (unsigned i = 0; i < size; i++) |
| 1377 | Dest.AggregateVal[i].IntVal = APIntOps::RoundFloatToAPInt( |
| 1378 | Src.AggregateVal[i].FloatVal, DBitWidth); |
| 1379 | } else { |
| 1380 | for (unsigned i = 0; i < size; i++) |
| 1381 | Dest.AggregateVal[i].IntVal = APIntOps::RoundDoubleToAPInt( |
| 1382 | Src.AggregateVal[i].DoubleVal, DBitWidth); |
| 1383 | } |
| 1384 | } else { |
| 1385 | // scalar |
| 1386 | unsigned DBitWidth = cast<IntegerType>(DstTy)->getBitWidth(); |
| 1387 | assert(SrcTy->isFloatingPointTy() && "Invalid FPToSI instruction"); |
| 1388 | |
| 1389 | if (SrcTy->getTypeID() == Type::FloatTyID) |
| 1390 | Dest.IntVal = APIntOps::RoundFloatToAPInt(Src.FloatVal, DBitWidth); |
| 1391 | else { |
| 1392 | Dest.IntVal = APIntOps::RoundDoubleToAPInt(Src.DoubleVal, DBitWidth); |
| 1393 | } |
| 1394 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1395 | return Dest; |
| 1396 | } |
| 1397 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1398 | GenericValue Interpreter::executeUIToFPInst(Value *SrcVal, Type *DstTy, |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1399 | ExecutionContext &SF) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1400 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
Reid Spencer | e092936 | 2007-03-03 08:38:04 +0000 | [diff] [blame] | 1401 | |
Elena Demikhovsky | cf21d15 | 2013-08-05 12:17:06 +0000 | [diff] [blame] | 1402 | if (SrcVal->getType()->getTypeID() == Type::VectorTyID) { |
| 1403 | const Type *DstVecTy = DstTy->getScalarType(); |
| 1404 | unsigned size = Src.AggregateVal.size(); |
| 1405 | // the sizes of src and dst vectors must be equal |
| 1406 | Dest.AggregateVal.resize(size); |
| 1407 | |
| 1408 | if (DstVecTy->getTypeID() == Type::FloatTyID) { |
| 1409 | assert(DstVecTy->isFloatingPointTy() && "Invalid UIToFP instruction"); |
| 1410 | for (unsigned i = 0; i < size; i++) |
| 1411 | Dest.AggregateVal[i].FloatVal = |
| 1412 | APIntOps::RoundAPIntToFloat(Src.AggregateVal[i].IntVal); |
| 1413 | } else { |
| 1414 | for (unsigned i = 0; i < size; i++) |
| 1415 | Dest.AggregateVal[i].DoubleVal = |
| 1416 | APIntOps::RoundAPIntToDouble(Src.AggregateVal[i].IntVal); |
| 1417 | } |
| 1418 | } else { |
| 1419 | // scalar |
| 1420 | assert(DstTy->isFloatingPointTy() && "Invalid UIToFP instruction"); |
| 1421 | if (DstTy->getTypeID() == Type::FloatTyID) |
| 1422 | Dest.FloatVal = APIntOps::RoundAPIntToFloat(Src.IntVal); |
| 1423 | else { |
| 1424 | Dest.DoubleVal = APIntOps::RoundAPIntToDouble(Src.IntVal); |
| 1425 | } |
| 1426 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1427 | return Dest; |
| 1428 | } |
| 1429 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1430 | GenericValue Interpreter::executeSIToFPInst(Value *SrcVal, Type *DstTy, |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1431 | ExecutionContext &SF) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1432 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
Reid Spencer | e092936 | 2007-03-03 08:38:04 +0000 | [diff] [blame] | 1433 | |
Elena Demikhovsky | cf21d15 | 2013-08-05 12:17:06 +0000 | [diff] [blame] | 1434 | if (SrcVal->getType()->getTypeID() == Type::VectorTyID) { |
| 1435 | const Type *DstVecTy = DstTy->getScalarType(); |
| 1436 | unsigned size = Src.AggregateVal.size(); |
| 1437 | // the sizes of src and dst vectors must be equal |
| 1438 | Dest.AggregateVal.resize(size); |
| 1439 | |
| 1440 | if (DstVecTy->getTypeID() == Type::FloatTyID) { |
| 1441 | assert(DstVecTy->isFloatingPointTy() && "Invalid SIToFP instruction"); |
| 1442 | for (unsigned i = 0; i < size; i++) |
| 1443 | Dest.AggregateVal[i].FloatVal = |
| 1444 | APIntOps::RoundSignedAPIntToFloat(Src.AggregateVal[i].IntVal); |
| 1445 | } else { |
| 1446 | for (unsigned i = 0; i < size; i++) |
| 1447 | Dest.AggregateVal[i].DoubleVal = |
| 1448 | APIntOps::RoundSignedAPIntToDouble(Src.AggregateVal[i].IntVal); |
| 1449 | } |
| 1450 | } else { |
| 1451 | // scalar |
| 1452 | assert(DstTy->isFloatingPointTy() && "Invalid SIToFP instruction"); |
| 1453 | |
| 1454 | if (DstTy->getTypeID() == Type::FloatTyID) |
| 1455 | Dest.FloatVal = APIntOps::RoundSignedAPIntToFloat(Src.IntVal); |
| 1456 | else { |
| 1457 | Dest.DoubleVal = APIntOps::RoundSignedAPIntToDouble(Src.IntVal); |
| 1458 | } |
| 1459 | } |
| 1460 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1461 | return Dest; |
| 1462 | } |
| 1463 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1464 | GenericValue Interpreter::executePtrToIntInst(Value *SrcVal, Type *DstTy, |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1465 | ExecutionContext &SF) { |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 1466 | uint32_t DBitWidth = cast<IntegerType>(DstTy)->getBitWidth(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1467 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1468 | assert(SrcVal->getType()->isPointerTy() && "Invalid PtrToInt instruction"); |
Reid Spencer | e092936 | 2007-03-03 08:38:04 +0000 | [diff] [blame] | 1469 | |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 1470 | Dest.IntVal = APInt(DBitWidth, (intptr_t) Src.PointerVal); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1471 | return Dest; |
| 1472 | } |
| 1473 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1474 | GenericValue Interpreter::executeIntToPtrInst(Value *SrcVal, Type *DstTy, |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1475 | ExecutionContext &SF) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1476 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1477 | assert(DstTy->isPointerTy() && "Invalid PtrToInt instruction"); |
Reid Spencer | e092936 | 2007-03-03 08:38:04 +0000 | [diff] [blame] | 1478 | |
Chandler Carruth | 426c2bf | 2012-11-01 09:14:31 +0000 | [diff] [blame] | 1479 | uint32_t PtrSize = TD.getPointerSizeInBits(); |
Reid Spencer | 7553c34 | 2007-03-06 03:41:50 +0000 | [diff] [blame] | 1480 | if (PtrSize != Src.IntVal.getBitWidth()) |
Reid Spencer | 5763105 | 2007-03-06 03:46:41 +0000 | [diff] [blame] | 1481 | Src.IntVal = Src.IntVal.zextOrTrunc(PtrSize); |
Reid Spencer | 7553c34 | 2007-03-06 03:41:50 +0000 | [diff] [blame] | 1482 | |
Reid Spencer | 6bc6333 | 2007-04-26 18:19:35 +0000 | [diff] [blame] | 1483 | Dest.PointerVal = PointerTy(intptr_t(Src.IntVal.getZExtValue())); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1484 | return Dest; |
| 1485 | } |
| 1486 | |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1487 | GenericValue Interpreter::executeBitCastInst(Value *SrcVal, Type *DstTy, |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1488 | ExecutionContext &SF) { |
Elena Demikhovsky | cf21d15 | 2013-08-05 12:17:06 +0000 | [diff] [blame] | 1489 | |
| 1490 | // This instruction supports bitwise conversion of vectors to integers and |
| 1491 | // to vectors of other types (as long as they have the same size) |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1492 | Type *SrcTy = SrcVal->getType(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1493 | GenericValue Dest, Src = getOperandValue(SrcVal, SF); |
Elena Demikhovsky | cf21d15 | 2013-08-05 12:17:06 +0000 | [diff] [blame] | 1494 | |
| 1495 | if ((SrcTy->getTypeID() == Type::VectorTyID) || |
| 1496 | (DstTy->getTypeID() == Type::VectorTyID)) { |
| 1497 | // vector src bitcast to vector dst or vector src bitcast to scalar dst or |
| 1498 | // scalar src bitcast to vector dst |
| 1499 | bool isLittleEndian = TD.isLittleEndian(); |
| 1500 | GenericValue TempDst, TempSrc, SrcVec; |
| 1501 | const Type *SrcElemTy; |
| 1502 | const Type *DstElemTy; |
| 1503 | unsigned SrcBitSize; |
| 1504 | unsigned DstBitSize; |
| 1505 | unsigned SrcNum; |
| 1506 | unsigned DstNum; |
| 1507 | |
| 1508 | if (SrcTy->getTypeID() == Type::VectorTyID) { |
| 1509 | SrcElemTy = SrcTy->getScalarType(); |
| 1510 | SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 1511 | SrcNum = Src.AggregateVal.size(); |
| 1512 | SrcVec = Src; |
| 1513 | } else { |
| 1514 | // if src is scalar value, make it vector <1 x type> |
| 1515 | SrcElemTy = SrcTy; |
| 1516 | SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 1517 | SrcNum = 1; |
| 1518 | SrcVec.AggregateVal.push_back(Src); |
| 1519 | } |
| 1520 | |
| 1521 | if (DstTy->getTypeID() == Type::VectorTyID) { |
| 1522 | DstElemTy = DstTy->getScalarType(); |
| 1523 | DstBitSize = DstTy->getScalarSizeInBits(); |
| 1524 | DstNum = (SrcNum * SrcBitSize) / DstBitSize; |
| 1525 | } else { |
| 1526 | DstElemTy = DstTy; |
| 1527 | DstBitSize = DstTy->getPrimitiveSizeInBits(); |
| 1528 | DstNum = 1; |
| 1529 | } |
| 1530 | |
| 1531 | if (SrcNum * SrcBitSize != DstNum * DstBitSize) |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1532 | llvm_unreachable("Invalid BitCast"); |
Elena Demikhovsky | cf21d15 | 2013-08-05 12:17:06 +0000 | [diff] [blame] | 1533 | |
| 1534 | // If src is floating point, cast to integer first. |
| 1535 | TempSrc.AggregateVal.resize(SrcNum); |
| 1536 | if (SrcElemTy->isFloatTy()) { |
| 1537 | for (unsigned i = 0; i < SrcNum; i++) |
| 1538 | TempSrc.AggregateVal[i].IntVal = |
| 1539 | APInt::floatToBits(SrcVec.AggregateVal[i].FloatVal); |
| 1540 | |
| 1541 | } else if (SrcElemTy->isDoubleTy()) { |
| 1542 | for (unsigned i = 0; i < SrcNum; i++) |
| 1543 | TempSrc.AggregateVal[i].IntVal = |
| 1544 | APInt::doubleToBits(SrcVec.AggregateVal[i].DoubleVal); |
| 1545 | } else if (SrcElemTy->isIntegerTy()) { |
| 1546 | for (unsigned i = 0; i < SrcNum; i++) |
| 1547 | TempSrc.AggregateVal[i].IntVal = SrcVec.AggregateVal[i].IntVal; |
| 1548 | } else { |
| 1549 | // Pointers are not allowed as the element type of vector. |
| 1550 | llvm_unreachable("Invalid Bitcast"); |
| 1551 | } |
| 1552 | |
| 1553 | // now TempSrc is integer type vector |
| 1554 | if (DstNum < SrcNum) { |
| 1555 | // Example: bitcast <4 x i32> <i32 0, i32 1, i32 2, i32 3> to <2 x i64> |
| 1556 | unsigned Ratio = SrcNum / DstNum; |
| 1557 | unsigned SrcElt = 0; |
| 1558 | for (unsigned i = 0; i < DstNum; i++) { |
| 1559 | GenericValue Elt; |
| 1560 | Elt.IntVal = 0; |
| 1561 | Elt.IntVal = Elt.IntVal.zext(DstBitSize); |
| 1562 | unsigned ShiftAmt = isLittleEndian ? 0 : SrcBitSize * (Ratio - 1); |
| 1563 | for (unsigned j = 0; j < Ratio; j++) { |
| 1564 | APInt Tmp; |
| 1565 | Tmp = Tmp.zext(SrcBitSize); |
| 1566 | Tmp = TempSrc.AggregateVal[SrcElt++].IntVal; |
| 1567 | Tmp = Tmp.zext(DstBitSize); |
| 1568 | Tmp = Tmp.shl(ShiftAmt); |
| 1569 | ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize; |
| 1570 | Elt.IntVal |= Tmp; |
| 1571 | } |
| 1572 | TempDst.AggregateVal.push_back(Elt); |
| 1573 | } |
| 1574 | } else { |
| 1575 | // Example: bitcast <2 x i64> <i64 0, i64 1> to <4 x i32> |
| 1576 | unsigned Ratio = DstNum / SrcNum; |
| 1577 | for (unsigned i = 0; i < SrcNum; i++) { |
| 1578 | unsigned ShiftAmt = isLittleEndian ? 0 : DstBitSize * (Ratio - 1); |
| 1579 | for (unsigned j = 0; j < Ratio; j++) { |
| 1580 | GenericValue Elt; |
| 1581 | Elt.IntVal = Elt.IntVal.zext(SrcBitSize); |
| 1582 | Elt.IntVal = TempSrc.AggregateVal[i].IntVal; |
| 1583 | Elt.IntVal = Elt.IntVal.lshr(ShiftAmt); |
| 1584 | // it could be DstBitSize == SrcBitSize, so check it |
| 1585 | if (DstBitSize < SrcBitSize) |
| 1586 | Elt.IntVal = Elt.IntVal.trunc(DstBitSize); |
| 1587 | ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize; |
| 1588 | TempDst.AggregateVal.push_back(Elt); |
| 1589 | } |
| 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | // convert result from integer to specified type |
| 1594 | if (DstTy->getTypeID() == Type::VectorTyID) { |
| 1595 | if (DstElemTy->isDoubleTy()) { |
| 1596 | Dest.AggregateVal.resize(DstNum); |
| 1597 | for (unsigned i = 0; i < DstNum; i++) |
| 1598 | Dest.AggregateVal[i].DoubleVal = |
| 1599 | TempDst.AggregateVal[i].IntVal.bitsToDouble(); |
| 1600 | } else if (DstElemTy->isFloatTy()) { |
| 1601 | Dest.AggregateVal.resize(DstNum); |
| 1602 | for (unsigned i = 0; i < DstNum; i++) |
| 1603 | Dest.AggregateVal[i].FloatVal = |
| 1604 | TempDst.AggregateVal[i].IntVal.bitsToFloat(); |
| 1605 | } else { |
| 1606 | Dest = TempDst; |
| 1607 | } |
| 1608 | } else { |
| 1609 | if (DstElemTy->isDoubleTy()) |
| 1610 | Dest.DoubleVal = TempDst.AggregateVal[0].IntVal.bitsToDouble(); |
| 1611 | else if (DstElemTy->isFloatTy()) { |
| 1612 | Dest.FloatVal = TempDst.AggregateVal[0].IntVal.bitsToFloat(); |
| 1613 | } else { |
| 1614 | Dest.IntVal = TempDst.AggregateVal[0].IntVal; |
| 1615 | } |
| 1616 | } |
| 1617 | } else { // if ((SrcTy->getTypeID() == Type::VectorTyID) || |
| 1618 | // (DstTy->getTypeID() == Type::VectorTyID)) |
| 1619 | |
| 1620 | // scalar src bitcast to scalar dst |
| 1621 | if (DstTy->isPointerTy()) { |
| 1622 | assert(SrcTy->isPointerTy() && "Invalid BitCast"); |
| 1623 | Dest.PointerVal = Src.PointerVal; |
| 1624 | } else if (DstTy->isIntegerTy()) { |
| 1625 | if (SrcTy->isFloatTy()) |
| 1626 | Dest.IntVal = APInt::floatToBits(Src.FloatVal); |
| 1627 | else if (SrcTy->isDoubleTy()) { |
| 1628 | Dest.IntVal = APInt::doubleToBits(Src.DoubleVal); |
| 1629 | } else if (SrcTy->isIntegerTy()) { |
| 1630 | Dest.IntVal = Src.IntVal; |
| 1631 | } else { |
| 1632 | llvm_unreachable("Invalid BitCast"); |
| 1633 | } |
| 1634 | } else if (DstTy->isFloatTy()) { |
| 1635 | if (SrcTy->isIntegerTy()) |
| 1636 | Dest.FloatVal = Src.IntVal.bitsToFloat(); |
| 1637 | else { |
| 1638 | Dest.FloatVal = Src.FloatVal; |
| 1639 | } |
| 1640 | } else if (DstTy->isDoubleTy()) { |
| 1641 | if (SrcTy->isIntegerTy()) |
| 1642 | Dest.DoubleVal = Src.IntVal.bitsToDouble(); |
| 1643 | else { |
| 1644 | Dest.DoubleVal = Src.DoubleVal; |
| 1645 | } |
| 1646 | } else { |
| 1647 | llvm_unreachable("Invalid Bitcast"); |
| 1648 | } |
| 1649 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1650 | |
| 1651 | return Dest; |
| 1652 | } |
| 1653 | |
| 1654 | void Interpreter::visitTruncInst(TruncInst &I) { |
Chris Lattner | d7916e9 | 2003-05-10 21:22:39 +0000 | [diff] [blame] | 1655 | ExecutionContext &SF = ECStack.back(); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1656 | SetValue(&I, executeTruncInst(I.getOperand(0), I.getType(), SF), SF); |
| 1657 | } |
| 1658 | |
| 1659 | void Interpreter::visitSExtInst(SExtInst &I) { |
| 1660 | ExecutionContext &SF = ECStack.back(); |
| 1661 | SetValue(&I, executeSExtInst(I.getOperand(0), I.getType(), SF), SF); |
| 1662 | } |
| 1663 | |
| 1664 | void Interpreter::visitZExtInst(ZExtInst &I) { |
| 1665 | ExecutionContext &SF = ECStack.back(); |
| 1666 | SetValue(&I, executeZExtInst(I.getOperand(0), I.getType(), SF), SF); |
| 1667 | } |
| 1668 | |
| 1669 | void Interpreter::visitFPTruncInst(FPTruncInst &I) { |
| 1670 | ExecutionContext &SF = ECStack.back(); |
| 1671 | SetValue(&I, executeFPTruncInst(I.getOperand(0), I.getType(), SF), SF); |
| 1672 | } |
| 1673 | |
| 1674 | void Interpreter::visitFPExtInst(FPExtInst &I) { |
| 1675 | ExecutionContext &SF = ECStack.back(); |
| 1676 | SetValue(&I, executeFPExtInst(I.getOperand(0), I.getType(), SF), SF); |
| 1677 | } |
| 1678 | |
| 1679 | void Interpreter::visitUIToFPInst(UIToFPInst &I) { |
| 1680 | ExecutionContext &SF = ECStack.back(); |
| 1681 | SetValue(&I, executeUIToFPInst(I.getOperand(0), I.getType(), SF), SF); |
| 1682 | } |
| 1683 | |
| 1684 | void Interpreter::visitSIToFPInst(SIToFPInst &I) { |
| 1685 | ExecutionContext &SF = ECStack.back(); |
| 1686 | SetValue(&I, executeSIToFPInst(I.getOperand(0), I.getType(), SF), SF); |
| 1687 | } |
| 1688 | |
| 1689 | void Interpreter::visitFPToUIInst(FPToUIInst &I) { |
| 1690 | ExecutionContext &SF = ECStack.back(); |
| 1691 | SetValue(&I, executeFPToUIInst(I.getOperand(0), I.getType(), SF), SF); |
| 1692 | } |
| 1693 | |
| 1694 | void Interpreter::visitFPToSIInst(FPToSIInst &I) { |
| 1695 | ExecutionContext &SF = ECStack.back(); |
| 1696 | SetValue(&I, executeFPToSIInst(I.getOperand(0), I.getType(), SF), SF); |
| 1697 | } |
| 1698 | |
| 1699 | void Interpreter::visitPtrToIntInst(PtrToIntInst &I) { |
| 1700 | ExecutionContext &SF = ECStack.back(); |
| 1701 | SetValue(&I, executePtrToIntInst(I.getOperand(0), I.getType(), SF), SF); |
| 1702 | } |
| 1703 | |
| 1704 | void Interpreter::visitIntToPtrInst(IntToPtrInst &I) { |
| 1705 | ExecutionContext &SF = ECStack.back(); |
| 1706 | SetValue(&I, executeIntToPtrInst(I.getOperand(0), I.getType(), SF), SF); |
| 1707 | } |
| 1708 | |
| 1709 | void Interpreter::visitBitCastInst(BitCastInst &I) { |
| 1710 | ExecutionContext &SF = ECStack.back(); |
| 1711 | SetValue(&I, executeBitCastInst(I.getOperand(0), I.getType(), SF), SF); |
Chris Lattner | a34c568 | 2002-08-27 22:33:45 +0000 | [diff] [blame] | 1712 | } |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 1713 | |
Brian Gaeke | c1a2be1 | 2003-11-07 21:20:47 +0000 | [diff] [blame] | 1714 | #define IMPLEMENT_VAARG(TY) \ |
| 1715 | case Type::TY##TyID: Dest.TY##Val = Src.TY##Val; break |
| 1716 | |
| 1717 | void Interpreter::visitVAArgInst(VAArgInst &I) { |
| 1718 | ExecutionContext &SF = ECStack.back(); |
| 1719 | |
Brian Gaeke | 9d20b71 | 2004-02-25 23:01:48 +0000 | [diff] [blame] | 1720 | // Get the incoming valist parameter. LLI treats the valist as a |
| 1721 | // (ec-stack-depth var-arg-index) pair. |
Brian Gaeke | c1a2be1 | 2003-11-07 21:20:47 +0000 | [diff] [blame] | 1722 | GenericValue VAList = getOperandValue(I.getOperand(0), SF); |
Brian Gaeke | 9d20b71 | 2004-02-25 23:01:48 +0000 | [diff] [blame] | 1723 | GenericValue Dest; |
| 1724 | GenericValue Src = ECStack[VAList.UIntPairVal.first] |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 1725 | .VarArgs[VAList.UIntPairVal.second]; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1726 | Type *Ty = I.getType(); |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 1727 | switch (Ty->getTypeID()) { |
Jim Grosbach | cce07c9 | 2013-01-31 19:46:59 +0000 | [diff] [blame] | 1728 | case Type::IntegerTyID: |
| 1729 | Dest.IntVal = Src.IntVal; |
Jim Grosbach | cce07c9 | 2013-01-31 19:46:59 +0000 | [diff] [blame] | 1730 | break; |
Jim Grosbach | 9f285c6 | 2013-02-01 18:57:06 +0000 | [diff] [blame] | 1731 | IMPLEMENT_VAARG(Pointer); |
| 1732 | IMPLEMENT_VAARG(Float); |
| 1733 | IMPLEMENT_VAARG(Double); |
Brian Gaeke | c1a2be1 | 2003-11-07 21:20:47 +0000 | [diff] [blame] | 1734 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 1735 | dbgs() << "Unhandled dest type for vaarg instruction: " << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1736 | llvm_unreachable(nullptr); |
Brian Gaeke | c1a2be1 | 2003-11-07 21:20:47 +0000 | [diff] [blame] | 1737 | } |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 1738 | |
Brian Gaeke | c1a2be1 | 2003-11-07 21:20:47 +0000 | [diff] [blame] | 1739 | // Set the Value of this Instruction. |
| 1740 | SetValue(&I, Dest, SF); |
Andrew Lenharth | 558bc88 | 2005-06-18 18:34:52 +0000 | [diff] [blame] | 1741 | |
| 1742 | // Move the pointer to the next vararg. |
| 1743 | ++VAList.UIntPairVal.second; |
Brian Gaeke | c1a2be1 | 2003-11-07 21:20:47 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
Nadav Rotem | 953783e | 2013-04-01 15:53:30 +0000 | [diff] [blame] | 1746 | void Interpreter::visitExtractElementInst(ExtractElementInst &I) { |
| 1747 | ExecutionContext &SF = ECStack.back(); |
| 1748 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 1749 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
| 1750 | GenericValue Dest; |
| 1751 | |
| 1752 | Type *Ty = I.getType(); |
| 1753 | const unsigned indx = unsigned(Src2.IntVal.getZExtValue()); |
| 1754 | |
| 1755 | if(Src1.AggregateVal.size() > indx) { |
| 1756 | switch (Ty->getTypeID()) { |
| 1757 | default: |
| 1758 | dbgs() << "Unhandled destination type for extractelement instruction: " |
| 1759 | << *Ty << "\n"; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 1760 | llvm_unreachable(nullptr); |
Nadav Rotem | 953783e | 2013-04-01 15:53:30 +0000 | [diff] [blame] | 1761 | break; |
| 1762 | case Type::IntegerTyID: |
| 1763 | Dest.IntVal = Src1.AggregateVal[indx].IntVal; |
| 1764 | break; |
| 1765 | case Type::FloatTyID: |
| 1766 | Dest.FloatVal = Src1.AggregateVal[indx].FloatVal; |
| 1767 | break; |
| 1768 | case Type::DoubleTyID: |
| 1769 | Dest.DoubleVal = Src1.AggregateVal[indx].DoubleVal; |
| 1770 | break; |
| 1771 | } |
| 1772 | } else { |
| 1773 | dbgs() << "Invalid index in extractelement instruction\n"; |
| 1774 | } |
| 1775 | |
| 1776 | SetValue(&I, Dest, SF); |
| 1777 | } |
| 1778 | |
Elena Demikhovsky | 4ca0ce2 | 2013-09-02 06:40:09 +0000 | [diff] [blame] | 1779 | void Interpreter::visitInsertElementInst(InsertElementInst &I) { |
| 1780 | ExecutionContext &SF = ECStack.back(); |
| 1781 | Type *Ty = I.getType(); |
| 1782 | |
| 1783 | if(!(Ty->isVectorTy()) ) |
| 1784 | llvm_unreachable("Unhandled dest type for insertelement instruction"); |
| 1785 | |
| 1786 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 1787 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
| 1788 | GenericValue Src3 = getOperandValue(I.getOperand(2), SF); |
| 1789 | GenericValue Dest; |
| 1790 | |
| 1791 | Type *TyContained = Ty->getContainedType(0); |
| 1792 | |
| 1793 | const unsigned indx = unsigned(Src3.IntVal.getZExtValue()); |
| 1794 | Dest.AggregateVal = Src1.AggregateVal; |
| 1795 | |
| 1796 | if(Src1.AggregateVal.size() <= indx) |
| 1797 | llvm_unreachable("Invalid index in insertelement instruction"); |
| 1798 | switch (TyContained->getTypeID()) { |
| 1799 | default: |
| 1800 | llvm_unreachable("Unhandled dest type for insertelement instruction"); |
| 1801 | case Type::IntegerTyID: |
| 1802 | Dest.AggregateVal[indx].IntVal = Src2.IntVal; |
| 1803 | break; |
| 1804 | case Type::FloatTyID: |
| 1805 | Dest.AggregateVal[indx].FloatVal = Src2.FloatVal; |
| 1806 | break; |
| 1807 | case Type::DoubleTyID: |
| 1808 | Dest.AggregateVal[indx].DoubleVal = Src2.DoubleVal; |
| 1809 | break; |
| 1810 | } |
| 1811 | SetValue(&I, Dest, SF); |
| 1812 | } |
| 1813 | |
| 1814 | void Interpreter::visitShuffleVectorInst(ShuffleVectorInst &I){ |
| 1815 | ExecutionContext &SF = ECStack.back(); |
| 1816 | |
| 1817 | Type *Ty = I.getType(); |
| 1818 | if(!(Ty->isVectorTy())) |
| 1819 | llvm_unreachable("Unhandled dest type for shufflevector instruction"); |
| 1820 | |
| 1821 | GenericValue Src1 = getOperandValue(I.getOperand(0), SF); |
| 1822 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
| 1823 | GenericValue Src3 = getOperandValue(I.getOperand(2), SF); |
| 1824 | GenericValue Dest; |
| 1825 | |
| 1826 | // There is no need to check types of src1 and src2, because the compiled |
| 1827 | // bytecode can't contain different types for src1 and src2 for a |
| 1828 | // shufflevector instruction. |
| 1829 | |
| 1830 | Type *TyContained = Ty->getContainedType(0); |
| 1831 | unsigned src1Size = (unsigned)Src1.AggregateVal.size(); |
| 1832 | unsigned src2Size = (unsigned)Src2.AggregateVal.size(); |
| 1833 | unsigned src3Size = (unsigned)Src3.AggregateVal.size(); |
| 1834 | |
| 1835 | Dest.AggregateVal.resize(src3Size); |
| 1836 | |
| 1837 | switch (TyContained->getTypeID()) { |
| 1838 | default: |
| 1839 | llvm_unreachable("Unhandled dest type for insertelement instruction"); |
| 1840 | break; |
| 1841 | case Type::IntegerTyID: |
| 1842 | for( unsigned i=0; i<src3Size; i++) { |
| 1843 | unsigned j = Src3.AggregateVal[i].IntVal.getZExtValue(); |
| 1844 | if(j < src1Size) |
| 1845 | Dest.AggregateVal[i].IntVal = Src1.AggregateVal[j].IntVal; |
| 1846 | else if(j < src1Size + src2Size) |
| 1847 | Dest.AggregateVal[i].IntVal = Src2.AggregateVal[j-src1Size].IntVal; |
| 1848 | else |
| 1849 | // The selector may not be greater than sum of lengths of first and |
| 1850 | // second operands and llasm should not allow situation like |
| 1851 | // %tmp = shufflevector <2 x i32> <i32 3, i32 4>, <2 x i32> undef, |
| 1852 | // <2 x i32> < i32 0, i32 5 >, |
| 1853 | // where i32 5 is invalid, but let it be additional check here: |
| 1854 | llvm_unreachable("Invalid mask in shufflevector instruction"); |
| 1855 | } |
| 1856 | break; |
| 1857 | case Type::FloatTyID: |
| 1858 | for( unsigned i=0; i<src3Size; i++) { |
| 1859 | unsigned j = Src3.AggregateVal[i].IntVal.getZExtValue(); |
| 1860 | if(j < src1Size) |
| 1861 | Dest.AggregateVal[i].FloatVal = Src1.AggregateVal[j].FloatVal; |
| 1862 | else if(j < src1Size + src2Size) |
| 1863 | Dest.AggregateVal[i].FloatVal = Src2.AggregateVal[j-src1Size].FloatVal; |
| 1864 | else |
| 1865 | llvm_unreachable("Invalid mask in shufflevector instruction"); |
| 1866 | } |
| 1867 | break; |
| 1868 | case Type::DoubleTyID: |
| 1869 | for( unsigned i=0; i<src3Size; i++) { |
| 1870 | unsigned j = Src3.AggregateVal[i].IntVal.getZExtValue(); |
| 1871 | if(j < src1Size) |
| 1872 | Dest.AggregateVal[i].DoubleVal = Src1.AggregateVal[j].DoubleVal; |
| 1873 | else if(j < src1Size + src2Size) |
| 1874 | Dest.AggregateVal[i].DoubleVal = |
| 1875 | Src2.AggregateVal[j-src1Size].DoubleVal; |
| 1876 | else |
| 1877 | llvm_unreachable("Invalid mask in shufflevector instruction"); |
| 1878 | } |
| 1879 | break; |
| 1880 | } |
| 1881 | SetValue(&I, Dest, SF); |
| 1882 | } |
| 1883 | |
Elena Demikhovsky | 3c5ce29 | 2013-09-12 10:48:23 +0000 | [diff] [blame] | 1884 | void Interpreter::visitExtractValueInst(ExtractValueInst &I) { |
| 1885 | ExecutionContext &SF = ECStack.back(); |
| 1886 | Value *Agg = I.getAggregateOperand(); |
| 1887 | GenericValue Dest; |
| 1888 | GenericValue Src = getOperandValue(Agg, SF); |
| 1889 | |
| 1890 | ExtractValueInst::idx_iterator IdxBegin = I.idx_begin(); |
| 1891 | unsigned Num = I.getNumIndices(); |
| 1892 | GenericValue *pSrc = &Src; |
| 1893 | |
| 1894 | for (unsigned i = 0 ; i < Num; ++i) { |
| 1895 | pSrc = &pSrc->AggregateVal[*IdxBegin]; |
| 1896 | ++IdxBegin; |
| 1897 | } |
| 1898 | |
| 1899 | Type *IndexedType = ExtractValueInst::getIndexedType(Agg->getType(), I.getIndices()); |
| 1900 | switch (IndexedType->getTypeID()) { |
| 1901 | default: |
| 1902 | llvm_unreachable("Unhandled dest type for extractelement instruction"); |
| 1903 | break; |
| 1904 | case Type::IntegerTyID: |
| 1905 | Dest.IntVal = pSrc->IntVal; |
| 1906 | break; |
| 1907 | case Type::FloatTyID: |
| 1908 | Dest.FloatVal = pSrc->FloatVal; |
| 1909 | break; |
| 1910 | case Type::DoubleTyID: |
| 1911 | Dest.DoubleVal = pSrc->DoubleVal; |
| 1912 | break; |
| 1913 | case Type::ArrayTyID: |
| 1914 | case Type::StructTyID: |
| 1915 | case Type::VectorTyID: |
| 1916 | Dest.AggregateVal = pSrc->AggregateVal; |
| 1917 | break; |
| 1918 | case Type::PointerTyID: |
| 1919 | Dest.PointerVal = pSrc->PointerVal; |
| 1920 | break; |
| 1921 | } |
| 1922 | |
| 1923 | SetValue(&I, Dest, SF); |
| 1924 | } |
| 1925 | |
| 1926 | void Interpreter::visitInsertValueInst(InsertValueInst &I) { |
| 1927 | |
| 1928 | ExecutionContext &SF = ECStack.back(); |
| 1929 | Value *Agg = I.getAggregateOperand(); |
| 1930 | |
| 1931 | GenericValue Src1 = getOperandValue(Agg, SF); |
| 1932 | GenericValue Src2 = getOperandValue(I.getOperand(1), SF); |
| 1933 | GenericValue Dest = Src1; // Dest is a slightly changed Src1 |
| 1934 | |
| 1935 | ExtractValueInst::idx_iterator IdxBegin = I.idx_begin(); |
| 1936 | unsigned Num = I.getNumIndices(); |
| 1937 | |
| 1938 | GenericValue *pDest = &Dest; |
| 1939 | for (unsigned i = 0 ; i < Num; ++i) { |
| 1940 | pDest = &pDest->AggregateVal[*IdxBegin]; |
| 1941 | ++IdxBegin; |
| 1942 | } |
| 1943 | // pDest points to the target value in the Dest now |
| 1944 | |
| 1945 | Type *IndexedType = ExtractValueInst::getIndexedType(Agg->getType(), I.getIndices()); |
| 1946 | |
| 1947 | switch (IndexedType->getTypeID()) { |
| 1948 | default: |
| 1949 | llvm_unreachable("Unhandled dest type for insertelement instruction"); |
| 1950 | break; |
| 1951 | case Type::IntegerTyID: |
| 1952 | pDest->IntVal = Src2.IntVal; |
| 1953 | break; |
| 1954 | case Type::FloatTyID: |
| 1955 | pDest->FloatVal = Src2.FloatVal; |
| 1956 | break; |
| 1957 | case Type::DoubleTyID: |
| 1958 | pDest->DoubleVal = Src2.DoubleVal; |
| 1959 | break; |
| 1960 | case Type::ArrayTyID: |
| 1961 | case Type::StructTyID: |
| 1962 | case Type::VectorTyID: |
| 1963 | pDest->AggregateVal = Src2.AggregateVal; |
| 1964 | break; |
| 1965 | case Type::PointerTyID: |
| 1966 | pDest->PointerVal = Src2.PointerVal; |
| 1967 | break; |
| 1968 | } |
| 1969 | |
| 1970 | SetValue(&I, Dest, SF); |
| 1971 | } |
| 1972 | |
Reid Spencer | e1aa066 | 2007-03-03 06:22:22 +0000 | [diff] [blame] | 1973 | GenericValue Interpreter::getConstantExprValue (ConstantExpr *CE, |
| 1974 | ExecutionContext &SF) { |
| 1975 | switch (CE->getOpcode()) { |
Elena Demikhovsky | 4ca0ce2 | 2013-09-02 06:40:09 +0000 | [diff] [blame] | 1976 | case Instruction::Trunc: |
Reid Spencer | e1aa066 | 2007-03-03 06:22:22 +0000 | [diff] [blame] | 1977 | return executeTruncInst(CE->getOperand(0), CE->getType(), SF); |
| 1978 | case Instruction::ZExt: |
| 1979 | return executeZExtInst(CE->getOperand(0), CE->getType(), SF); |
| 1980 | case Instruction::SExt: |
| 1981 | return executeSExtInst(CE->getOperand(0), CE->getType(), SF); |
| 1982 | case Instruction::FPTrunc: |
| 1983 | return executeFPTruncInst(CE->getOperand(0), CE->getType(), SF); |
| 1984 | case Instruction::FPExt: |
| 1985 | return executeFPExtInst(CE->getOperand(0), CE->getType(), SF); |
| 1986 | case Instruction::UIToFP: |
| 1987 | return executeUIToFPInst(CE->getOperand(0), CE->getType(), SF); |
| 1988 | case Instruction::SIToFP: |
| 1989 | return executeSIToFPInst(CE->getOperand(0), CE->getType(), SF); |
| 1990 | case Instruction::FPToUI: |
| 1991 | return executeFPToUIInst(CE->getOperand(0), CE->getType(), SF); |
| 1992 | case Instruction::FPToSI: |
| 1993 | return executeFPToSIInst(CE->getOperand(0), CE->getType(), SF); |
| 1994 | case Instruction::PtrToInt: |
| 1995 | return executePtrToIntInst(CE->getOperand(0), CE->getType(), SF); |
| 1996 | case Instruction::IntToPtr: |
| 1997 | return executeIntToPtrInst(CE->getOperand(0), CE->getType(), SF); |
| 1998 | case Instruction::BitCast: |
| 1999 | return executeBitCastInst(CE->getOperand(0), CE->getType(), SF); |
| 2000 | case Instruction::GetElementPtr: |
| 2001 | return executeGEPOperation(CE->getOperand(0), gep_type_begin(CE), |
| 2002 | gep_type_end(CE), SF); |
Reid Spencer | e092936 | 2007-03-03 08:38:04 +0000 | [diff] [blame] | 2003 | case Instruction::FCmp: |
| 2004 | case Instruction::ICmp: |
| 2005 | return executeCmpInst(CE->getPredicate(), |
| 2006 | getOperandValue(CE->getOperand(0), SF), |
| 2007 | getOperandValue(CE->getOperand(1), SF), |
| 2008 | CE->getOperand(0)->getType()); |
| 2009 | case Instruction::Select: |
| 2010 | return executeSelectInst(getOperandValue(CE->getOperand(0), SF), |
| 2011 | getOperandValue(CE->getOperand(1), SF), |
Elena Demikhovsky | 4ca0ce2 | 2013-09-02 06:40:09 +0000 | [diff] [blame] | 2012 | getOperandValue(CE->getOperand(2), SF), |
| 2013 | CE->getOperand(0)->getType()); |
Reid Spencer | e1aa066 | 2007-03-03 06:22:22 +0000 | [diff] [blame] | 2014 | default : |
| 2015 | break; |
| 2016 | } |
Reid Spencer | e092936 | 2007-03-03 08:38:04 +0000 | [diff] [blame] | 2017 | |
| 2018 | // The cases below here require a GenericValue parameter for the result |
| 2019 | // so we initialize one, compute it and then return it. |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 2020 | GenericValue Op0 = getOperandValue(CE->getOperand(0), SF); |
| 2021 | GenericValue Op1 = getOperandValue(CE->getOperand(1), SF); |
Reid Spencer | e1aa066 | 2007-03-03 06:22:22 +0000 | [diff] [blame] | 2022 | GenericValue Dest; |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2023 | Type * Ty = CE->getOperand(0)->getType(); |
Reid Spencer | e1aa066 | 2007-03-03 06:22:22 +0000 | [diff] [blame] | 2024 | switch (CE->getOpcode()) { |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2025 | case Instruction::Add: Dest.IntVal = Op0.IntVal + Op1.IntVal; break; |
| 2026 | case Instruction::Sub: Dest.IntVal = Op0.IntVal - Op1.IntVal; break; |
| 2027 | case Instruction::Mul: Dest.IntVal = Op0.IntVal * Op1.IntVal; break; |
| 2028 | case Instruction::FAdd: executeFAddInst(Dest, Op0, Op1, Ty); break; |
| 2029 | case Instruction::FSub: executeFSubInst(Dest, Op0, Op1, Ty); break; |
| 2030 | case Instruction::FMul: executeFMulInst(Dest, Op0, Op1, Ty); break; |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 2031 | case Instruction::FDiv: executeFDivInst(Dest, Op0, Op1, Ty); break; |
| 2032 | case Instruction::FRem: executeFRemInst(Dest, Op0, Op1, Ty); break; |
| 2033 | case Instruction::SDiv: Dest.IntVal = Op0.IntVal.sdiv(Op1.IntVal); break; |
| 2034 | case Instruction::UDiv: Dest.IntVal = Op0.IntVal.udiv(Op1.IntVal); break; |
| 2035 | case Instruction::URem: Dest.IntVal = Op0.IntVal.urem(Op1.IntVal); break; |
| 2036 | case Instruction::SRem: Dest.IntVal = Op0.IntVal.srem(Op1.IntVal); break; |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 2037 | case Instruction::And: Dest.IntVal = Op0.IntVal & Op1.IntVal; break; |
| 2038 | case Instruction::Or: Dest.IntVal = Op0.IntVal | Op1.IntVal; break; |
| 2039 | case Instruction::Xor: Dest.IntVal = Op0.IntVal ^ Op1.IntVal; break; |
Reid Spencer | f953633 | 2007-03-06 03:09:31 +0000 | [diff] [blame] | 2040 | case Instruction::Shl: |
| 2041 | Dest.IntVal = Op0.IntVal.shl(Op1.IntVal.getZExtValue()); |
| 2042 | break; |
| 2043 | case Instruction::LShr: |
| 2044 | Dest.IntVal = Op0.IntVal.lshr(Op1.IntVal.getZExtValue()); |
| 2045 | break; |
| 2046 | case Instruction::AShr: |
| 2047 | Dest.IntVal = Op0.IntVal.ashr(Op1.IntVal.getZExtValue()); |
| 2048 | break; |
Reid Spencer | e1aa066 | 2007-03-03 06:22:22 +0000 | [diff] [blame] | 2049 | default: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 2050 | dbgs() << "Unhandled ConstantExpr: " << *CE << "\n"; |
Ahmed Charles | b0934ab | 2012-02-19 11:37:01 +0000 | [diff] [blame] | 2051 | llvm_unreachable("Unhandled ConstantExpr"); |
Reid Spencer | e1aa066 | 2007-03-03 06:22:22 +0000 | [diff] [blame] | 2052 | } |
Reid Spencer | e092936 | 2007-03-03 08:38:04 +0000 | [diff] [blame] | 2053 | return Dest; |
Reid Spencer | e1aa066 | 2007-03-03 06:22:22 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | GenericValue Interpreter::getOperandValue(Value *V, ExecutionContext &SF) { |
| 2057 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) { |
| 2058 | return getConstantExprValue(CE, SF); |
| 2059 | } else if (Constant *CPV = dyn_cast<Constant>(V)) { |
| 2060 | return getConstantValue(CPV); |
| 2061 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 2062 | return PTOGV(getPointerToGlobal(GV)); |
| 2063 | } else { |
| 2064 | return SF.Values[V]; |
| 2065 | } |
| 2066 | } |
| 2067 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 2068 | //===----------------------------------------------------------------------===// |
| 2069 | // Dispatch and Execution Code |
| 2070 | //===----------------------------------------------------------------------===// |
| 2071 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 2072 | //===----------------------------------------------------------------------===// |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 2073 | // callFunction - Execute the specified function... |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 2074 | // |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 2075 | void Interpreter::callFunction(Function *F, |
| 2076 | const std::vector<GenericValue> &ArgVals) { |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 2077 | assert((ECStack.empty() || !ECStack.back().Caller.getInstruction() || |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 2078 | ECStack.back().Caller.arg_size() == ArgVals.size()) && |
| 2079 | "Incorrect number of arguments passed into function call!"); |
Chris Lattner | 63bd613 | 2003-09-17 17:26:22 +0000 | [diff] [blame] | 2080 | // Make a new stack frame... and fill it in. |
| 2081 | ECStack.push_back(ExecutionContext()); |
| 2082 | ExecutionContext &StackFrame = ECStack.back(); |
Chris Lattner | da82ed5 | 2003-05-08 16:18:31 +0000 | [diff] [blame] | 2083 | StackFrame.CurFunction = F; |
Brian Gaeke | af955ba | 2003-11-07 05:22:49 +0000 | [diff] [blame] | 2084 | |
| 2085 | // Special handling for external functions. |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 2086 | if (F->isDeclaration()) { |
Brian Gaeke | af955ba | 2003-11-07 05:22:49 +0000 | [diff] [blame] | 2087 | GenericValue Result = callExternalFunction (F, ArgVals); |
| 2088 | // Simulate a 'ret' instruction of the appropriate type. |
| 2089 | popStackAndReturnValueToCaller (F->getReturnType (), Result); |
| 2090 | return; |
| 2091 | } |
| 2092 | |
| 2093 | // Get pointers to first LLVM BB & Instruction in function. |
Chris Lattner | cdf5178 | 2003-05-08 16:06:52 +0000 | [diff] [blame] | 2094 | StackFrame.CurBB = F->begin(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 2095 | StackFrame.CurInst = StackFrame.CurBB->begin(); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 2096 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 2097 | // Run through the function arguments and initialize their values... |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 2098 | assert((ArgVals.size() == F->arg_size() || |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 2099 | (ArgVals.size() > F->arg_size() && F->getFunctionType()->isVarArg()))&& |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 2100 | "Invalid number of values passed to function invocation!"); |
Chris Lattner | cdf5178 | 2003-05-08 16:06:52 +0000 | [diff] [blame] | 2101 | |
| 2102 | // Handle non-varargs arguments... |
Chris Lattner | 365a76e | 2001-09-10 04:49:44 +0000 | [diff] [blame] | 2103 | unsigned i = 0; |
Reid Spencer | 4ccf462 | 2007-04-16 21:50:40 +0000 | [diff] [blame] | 2104 | for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end(); |
| 2105 | AI != E; ++AI, ++i) |
Chris Lattner | 0b12b5f | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 2106 | SetValue(AI, ArgVals[i], StackFrame); |
Chris Lattner | cdf5178 | 2003-05-08 16:06:52 +0000 | [diff] [blame] | 2107 | |
| 2108 | // Handle varargs arguments... |
| 2109 | StackFrame.VarArgs.assign(ArgVals.begin()+i, ArgVals.end()); |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
Reid Spencer | 951418b | 2007-05-16 02:05:13 +0000 | [diff] [blame] | 2112 | |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 2113 | void Interpreter::run() { |
Brian Gaeke | 9ad671d | 2003-09-04 23:15:40 +0000 | [diff] [blame] | 2114 | while (!ECStack.empty()) { |
Brian Gaeke | 03e43dc | 2003-10-24 19:59:01 +0000 | [diff] [blame] | 2115 | // Interpret a single instruction & increment the "PC". |
| 2116 | ExecutionContext &SF = ECStack.back(); // Current stack frame |
| 2117 | Instruction &I = *SF.CurInst++; // Increment before execute |
Misha Brukman | d1c881a | 2005-04-21 22:43:08 +0000 | [diff] [blame] | 2118 | |
Brian Gaeke | 03e43dc | 2003-10-24 19:59:01 +0000 | [diff] [blame] | 2119 | // Track the number of dynamic instructions executed. |
| 2120 | ++NumDynamicInsts; |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 2121 | |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 2122 | DEBUG(dbgs() << "About to interpret: " << I); |
Brian Gaeke | 03e43dc | 2003-10-24 19:59:01 +0000 | [diff] [blame] | 2123 | visit(I); // Dispatch to one of the visit* methods... |
Chris Lattner | 27725bf | 2007-09-21 18:30:39 +0000 | [diff] [blame] | 2124 | #if 0 |
| 2125 | // This is not safe, as visiting the instruction could lower it and free I. |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 2126 | DEBUG( |
Reid Spencer | 951418b | 2007-05-16 02:05:13 +0000 | [diff] [blame] | 2127 | if (!isa<CallInst>(I) && !isa<InvokeInst>(I) && |
| 2128 | I.getType() != Type::VoidTy) { |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 2129 | dbgs() << " --> "; |
Chris Lattner | 27725bf | 2007-09-21 18:30:39 +0000 | [diff] [blame] | 2130 | const GenericValue &Val = SF.Values[&I]; |
| 2131 | switch (I.getType()->getTypeID()) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2132 | default: llvm_unreachable("Invalid GenericValue Type"); |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 2133 | case Type::VoidTyID: dbgs() << "void"; break; |
| 2134 | case Type::FloatTyID: dbgs() << "float " << Val.FloatVal; break; |
| 2135 | case Type::DoubleTyID: dbgs() << "double " << Val.DoubleVal; break; |
| 2136 | case Type::PointerTyID: dbgs() << "void* " << intptr_t(Val.PointerVal); |
Chris Lattner | 27725bf | 2007-09-21 18:30:39 +0000 | [diff] [blame] | 2137 | break; |
| 2138 | case Type::IntegerTyID: |
David Greene | ef573a3 | 2010-01-05 01:27:23 +0000 | [diff] [blame] | 2139 | dbgs() << "i" << Val.IntVal.getBitWidth() << " " |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 2140 | << Val.IntVal.toStringUnsigned(10) |
| 2141 | << " (0x" << Val.IntVal.toStringUnsigned(16) << ")\n"; |
Chris Lattner | 27725bf | 2007-09-21 18:30:39 +0000 | [diff] [blame] | 2142 | break; |
| 2143 | } |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 2144 | }); |
Chris Lattner | 27725bf | 2007-09-21 18:30:39 +0000 | [diff] [blame] | 2145 | #endif |
Chris Lattner | 92101ac | 2001-08-23 17:05:04 +0000 | [diff] [blame] | 2146 | } |
| 2147 | } |